Home | History | Annotate | Download | only in camera
      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 android.app.Activity;
     20 import android.hardware.Camera;
     21 import android.os.Bundle;
     22 import android.util.Log;
     23 import android.widget.FrameLayout;
     24 import android.widget.Toast;
     25 
     26 import com.example.android.basicpermissions.R;
     27 
     28 /**
     29  * Displays a {@link CameraPreview} of the first {@link Camera}.
     30  * An error message is displayed if the Camera is not available.
     31  * <p>
     32  * This Activity is only used to illustrate that access to the Camera API has been granted (or
     33  * denied) as part of the runtime permissions model. It is not relevant for the use of the
     34  * permissions API.
     35  * <p>
     36  * Implementation is based directly on the documentation at
     37  * http://developer.android.com/guide/topics/media/camera.html
     38  */
     39 public class CameraPreviewActivity extends Activity {
     40     private static final String TAG = "CameraPreviewActivity";
     41     /**
     42      * Id of the camera to access. 0 is the first camera.
     43      */
     44     private static final int CAMERA_ID = 0;
     45 
     46     private Camera mCamera;
     47 
     48     @Override
     49     protected void onCreate(Bundle savedInstanceState) {
     50         super.onCreate(savedInstanceState);
     51 
     52         // Open an instance of the first camera and retrieve its info.
     53         mCamera = getCameraInstance(CAMERA_ID);
     54         Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
     55         Camera.getCameraInfo(CAMERA_ID, cameraInfo);
     56 
     57         if (mCamera == null) {
     58             // Camera is not available, display error message
     59             setContentView(R.layout.activity_camera_unavailable);
     60         } else {
     61 
     62             setContentView(R.layout.activity_camera);
     63 
     64             // Get the rotation of the screen to adjust the preview image accordingly.
     65             int displayRotation = getWindowManager().getDefaultDisplay().getRotation();
     66 
     67             // Create the Preview view and set it as the content of this Activity.
     68             CameraPreview cameraPreview = new CameraPreview(this, null,
     69                     0, mCamera, cameraInfo, displayRotation);
     70             FrameLayout preview = findViewById(R.id.camera_preview);
     71             preview.addView(cameraPreview);
     72         }
     73     }
     74 
     75     @Override
     76     public void onPause() {
     77         super.onPause();
     78         // Stop camera access
     79         releaseCamera();
     80     }
     81 
     82     /**
     83      * A safe way to get an instance of the Camera object.
     84      */
     85     private Camera getCameraInstance(int cameraId) {
     86         Camera c = null;
     87         try {
     88             c = Camera.open(cameraId); // attempt to get a Camera instance
     89         } catch (Exception e) {
     90             // Camera is not available (in use or does not exist)
     91             Log.e(TAG, "Camera " + cameraId + " is not available: " + e.getMessage());
     92         }
     93         return c; // returns null if camera is unavailable
     94     }
     95 
     96     /**
     97      * Release the camera for other applications.
     98      */
     99     private void releaseCamera() {
    100         if (mCamera != null) {
    101             mCamera.release();
    102             mCamera = null;
    103         }
    104     }
    105 }
    106