Download presentation
Presentation is loading. Please wait.
1
CIS 470 Mobile App Development
Lecture 18 Wenbing Zhao Department of Electrical Engineering and Computer Science Cleveland State University 11/7/2018 CIS 470: Mobile App Development
2
CIS 470: Mobile App Development
Using the Camera Take a picture Live view 11/7/2018 CIS 470: Mobile App Development
3
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=" 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” <Button android:text="Capture” android:layout_height="wrap_content” android:layout_width="wrap_content" android:enabled="true"></Button> </LinearLayout> <ImageView 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
4
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
5
CIS 470: Mobile App Development
Basic Camera Usage Modify MainActivity.java private void basicCamera() { mButtonCapture = (Button) findViewById(R.id.btnCapture); mButtonCapture.setOnClickListener(mCaptureListener); 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
6
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
7
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
8
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
9
CIS 470: Mobile App Development
Live View <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.wenbing.livecapture.Preview 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
10
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
11
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
12
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 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
13
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
14
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
15
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
16
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
17
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.