CIS 470 Mobile App Development

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial. Topics Lecture 5 Overview Overview of Networking Programming Tutorial 2: Downloading from the Internet.
Advertisements

Android – Camera –an Example
Getting Started with Android APIs Ivan Wong. Motivation - “Datasheet” - Recently exposed to what’s available in Android - So let’s see what API’s are.
Package org.androidtown.database.query; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase;
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
1/29/ Android Programming: FrameLayout By Dr. Ramji M. Makwana Professor and Head, Computer Engineering Department A.D. Patel.
Android Programming-Activity Lecture 4. Activity Inside java folder Public class MainActivity extends ActionBarActivity(Ctrl + Click will give you the.
Android - Broadcast Receivers
Import import android.graphics.Bitmap; import android.widget.ImageView;
로봇 모니터링 1/2 UNIT 20 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 Message Queue Handler 2.
Mobile Device Development Camera and Sensors Dr.YingLiang Ma.
Android and s Ken Nguyen Clayton state University 2012.
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Lab7 – Appendix.
Introduction to android
Lab7 – Advanced.
Permissions.
Android Programming - Features
Lecture 3 Zablon Ochomo Android Layouts Lecture 3 Zablon Ochomo
Android Introduction Hello World
Adapting to Display Orientation
GUI Programming Fundamentals
CS499 – Mobile Application Development
Android Introduction Hello World.
CS499 – Mobile Application Development
Android Widgets 1 7 August 2018
Android – Read/Write to External Storage
Android Introduction Camera.
Picasso Revisted.
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Android Sensor Programming
CIS 470 Mobile App Development
Many thanks to Jun Bum Lim for his help with this tutorial.
BMI Android Application will take weight and height from the users to calculate Body Mass Index (BMI) with the information, whether user is underweight,
Activities and Intents
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 493/EEC 492 Android Sensor Programming
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Android Notifications
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Adding Components to Activity
BLP 4216 MOBİL UYGULAMA GELİŞTİRME-2
CMPE419 Mobile Application Development
CIS 470 Mobile App Development
Mobile Programming Broadcast Receivers.
Android Sensor Programming
Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
CIS 470 Mobile App Development
CIS 694/EEC 693 Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

CIS 470 Mobile App Development Lecture 18 Wenbing Zhao Department of Electrical Engineering and Computer Science Cleveland State University wenbing@ieee.org 11/7/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Using the Camera Take a picture Live view 11/7/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Basic Camera Usage Create a new app and name it BasicCamera Add permission in manifest Modify activity_main.xml: <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent” android:layout_height="fill_parent” android:weightSum="1"> <LinearLayout android:layout_height="wrap_content” android:layout_width="match_parent” android:id="@+id/linearLayout1"> <Button android:id="@+id/btnCapture" android:text="Capture” android:layout_height="wrap_content” android:layout_width="wrap_content" android:enabled="true"></Button> </LinearLayout> <ImageView android:id="@+id/imageView1" android:layout_weight="0.69” android:layout_width="match_parent” android:layout_height="match_parent"> </ImageView> </LinearLayout> 11/7/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Basic Camera Usage @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 124); System.out.println("write permission not enabled"); return; } if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 125); System.out.println("read permission not enabled"); return; } basicCamera(); } Modify MainActivity.java import java.io.File; import java.io.IOException; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.support.v4.app.ActivityCompat; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.os.Environment; import android.support.v4.content.FileProvider; import android.os.StrictMode; public class MainActivity extends Activity { private final int PICTURE_ACTIVITY_CODE = 1; private final String FILENAME = "photo.jpg"; private Button mButtonCapture; private File mFile; 11/7/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Basic Camera Usage Modify MainActivity.java private void basicCamera() { mButtonCapture = (Button) findViewById(R.id.btnCapture); mButtonCapture.setOnClickListener(mCaptureListener); } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case 124: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { basicCamera(); } } } } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PICTURE_ACTIVITY_CODE) { if (resultCode == RESULT_OK) { ImageView imageView = (ImageView) findViewById(R.id.imageView1); Uri inputFileUri = Uri.fromFile(mFile); imageView.setImageURI(inputFileUri); } } } 11/7/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development private void launchTakePhoto() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File imagesFolder = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), "MyImages"); imagesFolder.mkdirs(); mFile = new File(imagesFolder, FILENAME); if(!mFile.exists()) { try { mFile.createNewFile(); } catch(IOException e) { e.printStackTrace(); } } Uri uriSavedImage = Uri.fromFile(mFile); intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); startActivityForResult(intent, PICTURE_ACTIVITY_CODE); } private OnClickListener mCaptureListener = new OnClickListener() { @Override public void onClick(View v) { launchTakePhoto(); } }; } 11/7/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Basic Camera Usage This app brings up the default camera app to take a picture. Then save it to a file and display it on screen 11/7/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Live View Build an app that display the live view (without the need of the default camera app). Name the app LiveCapture Add a new Java class and name it Preview. Reference the code posted separated in the course webpage (Preview.java) It is a wrapper around a Camera and a SurfaceView that renders a centered preview of the Camera to the surface Add permission to camera in manifest <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> 11/7/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Live View <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.wenbing.livecapture.Preview android:id="@+id/preview1" android:layout_width="match_parent" android:layout_height="400dp"> </com.wenbing.livecapture.Preview> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Switch to Front Camera" android:onClick="switchFrontCamera"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Switch to Back Camera" android:onClick="switchBackCamera"/> </LinearLayout> Modify activity_main.xml: 11/7/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development import android.app.Activity; import android.hardware.Camera; import android.hardware.Camera.CameraInfo; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.widget.Toast; import android.content.pm.PackageManager; import android.support.v4.app.ActivityCompat; import android.view.View; import android.view.Surface; Live View Modify MainActivity.java public class MainActivity extends Activity { private static final int MY_PERMISSIONS_REQUEST_CAMERA = 555; private static final String TAG = "LiveCaptureActivity"; Camera mCamera; private int mDefaultCameraId; private int mFrontfacingCameraId; private Preview mPreview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPreview = (Preview) findViewById(R.id.preview1); if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA); } else { liveCatpure(); } } 11/7/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Live View Modify MainActivity.java @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_CAMERA: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { liveCatpure(); } else { } } } } 11/7/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development private void liveCatpure() { int nCameras; if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.GINGERBREAD) { nCameras = Camera.getNumberOfCameras(); } else { nCameras = 1; mDefaultCameraId = 0; mFrontfacingCameraId = 0; } CameraInfo cameraInfo = new CameraInfo(); // Find the ID of the default camera if there is more than 1 for (int i = 0; i < nCameras; i++) { Camera.getCameraInfo(i, cameraInfo); if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) { mDefaultCameraId = i; } else if(cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) { mFrontfacingCameraId = i; } } if (mDefaultCameraId == -1) // test for no camera facing back { if (nCameras > 0) // test for no cameras { mDefaultCameraId = 0; } else { Toast toast = Toast.makeText(getApplicationContext(), "This device has no cameras!", Toast.LENGTH_LONG); toast.show(); finish(); } } } 11/7/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development @Override protected void onResume() { super.onResume(); Log.d(TAG, "CAMERA: opening camera: " + mDefaultCameraId); mCamera = Camera.open(mDefaultCameraId); mPreview.setCamera(mCamera); setCameraDisplayOrientation(mDefaultCameraId); } @Override protected void onPause() { super.onPause(); // Because the Camera object is a shared resource, it's very // important to release it when the activity is paused. if (mCamera != null) { mPreview.setCamera(null); mCamera.release(); mCamera = null; } } 11/7/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development public void switchFrontCamera(View view) { mCamera.startPreview(); mCamera.release(); mCamera = Camera.open(mFrontfacingCameraId); mPreview.switchCamera(mCamera); setCameraDisplayOrientation(mFrontfacingCameraId); mCamera.startPreview(); } public void switchBackCamera(View view) { mCamera.startPreview(); mCamera.release(); mCamera = Camera.open(mDefaultCameraId); mPreview.switchCamera(mCamera); setCameraDisplayOrientation(mDefaultCameraId); mCamera.startPreview(); } 11/7/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Android automatically rotates everything on the screen — text and so on — but it doesn’t automatically do this for the camera preview private void setCameraDisplayOrientation(int cameraId) { CameraInfo cameraInfo = new CameraInfo(); Camera.getCameraInfo(cameraId, cameraInfo); int rotation = getWindowManager().getDefaultDisplay().getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; case Surface.ROTATION_90: degrees = 90; break; case Surface.ROTATION_180: degrees = 180; break; case Surface.ROTATION_270: degrees = 270; break; } int desiredRotation = (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) ? (360 - cameraInfo.orientation) : cameraInfo.orientation; int result = (desiredRotation - degrees + 360) % 360; mCamera.setDisplayOrientation(result); } } 11/7/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Live View The Preview View: Preview is a View that shows a live preview of the camera image The key thing to understand about Preview (and showing camera previews in Android in general) is that it shows the camera preview in a SurfaceView field SurfaceView is the one class in Android that can show a camera preview. You must have a SurfaceView if you are going to use camera preview Preview makes setCamera() available to choose a particular camera to preview 11/7/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Challenge Tasks At least the front camera view is distorted. See if you can fix it. I noticed sometimes the app is not stable. Find out the reason why and fit it (if it happens to you). 11/7/2018 CIS 470: Mobile App Development