• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 package com.example.android.basicpermissions.camera;
18 
19 import com.example.android.basicpermissions.R;
20 
21 import android.app.Activity;
22 import android.hardware.Camera;
23 import android.os.Bundle;
24 import android.widget.FrameLayout;
25 import android.widget.Toast;
26 
27 /**
28  * Displays a {@link CameraPreview} of the first {@link Camera}.
29  * An error message is displayed if the Camera is not available.
30  * <p>
31  * This Activity is only used to illustrate that access to the Camera API has been granted (or
32  * denied) as part of the runtime permissions model. It is not relevant for the use of the
33  * permissions API.
34  * <p>
35  * Implementation is based directly on the documentation at
36  * http://developer.android.com/guide/topics/media/camera.html
37  */
38 public class CameraPreviewActivity extends Activity {
39 
40     private static final String TAG = "CameraPreview";
41 
42     /**
43      * Id of the camera to access. 0 is the first camera.
44      */
45     private static final int CAMERA_ID = 0;
46 
47     private CameraPreview mPreview;
48     private Camera mCamera;
49 
50     @Override
onCreate(Bundle savedInstanceState)51     protected void onCreate(Bundle savedInstanceState) {
52         super.onCreate(savedInstanceState);
53 
54         // Open an instance of the first camera and retrieve its info.
55         mCamera = getCameraInstance(CAMERA_ID);
56         Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
57         Camera.getCameraInfo(CAMERA_ID, cameraInfo);
58 
59         if (mCamera == null || cameraInfo == null) {
60             // Camera is not available, display error message
61             Toast.makeText(this, "Camera is not available.", Toast.LENGTH_SHORT).show();
62             setContentView(R.layout.activity_camera_unavailable);
63         } else {
64 
65             setContentView(R.layout.activity_camera);
66 
67             // Get the rotation of the screen to adjust the preview image accordingly.
68             final int displayRotation = getWindowManager().getDefaultDisplay()
69                     .getRotation();
70 
71             // Create the Preview view and set it as the content of this Activity.
72             mPreview = new CameraPreview(this, mCamera, cameraInfo, displayRotation);
73             FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
74             preview.addView(mPreview);
75         }
76     }
77 
78     @Override
onPause()79     public void onPause() {
80         super.onPause();
81         // Stop camera access
82         releaseCamera();
83     }
84 
85     /** A safe way to get an instance of the Camera object. */
getCameraInstance(int cameraId)86     private Camera getCameraInstance(int cameraId) {
87         Camera c = null;
88         try {
89             c = Camera.open(cameraId); // attempt to get a Camera instance
90         } catch (Exception e) {
91             // Camera is not available (in use or does not exist)
92             Toast.makeText(this, "Camera " + cameraId + " is not available: " + e.getMessage(),
93                     Toast.LENGTH_SHORT).show();
94         }
95         return c; // returns null if camera is unavailable
96     }
97 
releaseCamera()98     private void releaseCamera() {
99         if (mCamera != null) {
100             mCamera.release();        // release the camera for other applications
101             mCamera = null;
102         }
103     }
104 }
105