Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android studio for android learning (十五) Camera2界面定制與保存到外部存儲

android studio for android learning (十五) Camera2界面定制與保存到外部存儲

編輯:關於Android編程

1.使用android 5.0的Camera V2來控制手機拍照,5.0以後對拍照的API進行了全新的設計,這些API不僅大幅度提高了android的拍照功能,還支持RAW照片輸出,還可以程序調整相機的對焦模式,曝光模式,快門等。


2.關鍵API簡介

CameraManager:攝像頭管理器 CameraCharacteristics:攝像頭特性 CameraDevice:代表系統攝像頭 CameraCaptureSession:創建會話的API,是一個非常重要的API,當程序不管是拍照還是預覽都需要通過該API來創建會話,其中控制預覽的方法為setRepeatingRequest();控制拍照的方法為capture() 為了監聽cameraCaptureSession的創建過程和CameraCaptureSession的拍照過程API為CameraCaptureSession提供了stateCallback,CaptureCallback等內部類 CameraRequest代表一次捕獲請求,CameraRequest.Builder則負責後成CameraRequest對象。

3.本示例演示打開攝像頭通過camera2的API,將拍好的照片保存到外部存儲器SD中,其中控制拍照的大致步驟如下:

調用CameraManager的openCamera()方法,該方法有三個參數,第一個是代表要打開的攝像頭,第二個參數用於監聽攝像頭的狀態,第三個參數代表要執行的callback的Hander,如果在當前執行,可以設置為NULL 當調用上面的openCamera後,就可以獲取指定的系統攝像頭CameraDevice,然後調用指定的CameraDevice的createCaptureSession()來創建CameraCaptureSession,該方法有三個參數,第一個是一個LIST集合,封裝了所有需要從該攝像頭獲取圖片的surface,第二個參數用來監聽CameraCaptureSession的創建過程。第三個參數代表執行callback的handler.設置為null就是在本線程中執行。 調用cameraDevice中的createCaptureRequest()方法,該方法支持的請求參數包括預覽,拍攝視頻,拍照等 根據上面的方式來設置各種參數,這裡是設置各種拍照參數 調用captureRequest.Builder的build方法,即可得打為CaptureRequest對象,接下來可通過CameraCaptureSession的setRepeatingRuest()方法來開始預覽和拍照。

4示例代碼和相關文件

androidMainfest.xml




    

    
    
    
    
    


    
        
            
                

                
            
        
    

strings.xml


    Camera
    take picture

布局文件main.xml



    
    

代碼文件Main.java,核心代碼文件,以加注釋,大家可以慢慢看

package com.dragon.camera;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.TotalCaptureResult;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.Image;
import android.media.ImageReader;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.Size;
import android.util.SparseIntArray;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class Main extends Activity {
    private static final String TAG = " dragon camera test";
//    兩個組件
    private Button takePictureButton;
    private TextureView textureView;
//用SparseIntArray來代替hashMap,進行性能優化
    private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
    static {
        ORIENTATIONS.append(Surface.ROTATION_0, 90);
        ORIENTATIONS.append(Surface.ROTATION_90, 0);
        ORIENTATIONS.append(Surface.ROTATION_180, 270);
        ORIENTATIONS.append(Surface.ROTATION_270, 180);
    }
//    攝像頭ID,一般0是後視,1是前視
    private String cameraId;
//定義代表攝像頭的成員變量,代表系統攝像頭,該類的功能類似早期的Camera類。
    protected CameraDevice cameraDevice;
//    定義CameraCaptureSession成員變量,是一個拍攝繪話的類,用來從攝像頭拍攝圖像或是重新拍攝圖像,這是一個重要的API.
    protected CameraCaptureSession cameraCaptureSessions;
//    當程序調用setRepeatingRequest()方法進行預覽時,或調用capture()進行拍照時,都需要傳入CaptureRequest參數時
//    captureRequest代表一次捕獲請求,用於描述捕獲圖片的各種參數設置。比如對焦模式,曝光模式...等,程序對照片所做的各種控制,都通過CaptureRequest參數來進行設置
//    CaptureRequest.Builder 負責生成captureRequest對象
    protected CaptureRequest.Builder captureRequestBuilder;
//預覽尺寸
    private Size imageDimension;
//   ImageReader allow direct application access to image data rendered into a Surface.
    private ImageReader imageReader;
//請求碼常量,可以自定義
    private static final int REQUEST_CAMERA_PERMISSION = 300;
    private Handler mBackgroundHandler;
    private HandlerThread mBackgroundThread;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        textureView = (TextureView) findViewById(R.id.texture);
        assert textureView != null;
//        設置監聽
        textureView.setSurfaceTextureListener(textureListener);
        takePictureButton = (Button) findViewById(R.id.btn_takepicture);
//        為空時拋出異常
        assert takePictureButton != null;
        class takePictureBtn implements View.OnClickListener{
            public void onClick(View v) {
                takePicture();
            }
        }
        takePictureButton.setOnClickListener(new takePictureBtn());
    }

//    定義了一個獨立的監聽類
    TextureView.SurfaceTextureListener textureListener = new TextureView.SurfaceTextureListener() {
        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
            //open your camera here
            openCamera();
        }
        @Override
        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
            // Transform you image captured size according to the surface width and height
        }
        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
            return true;
        }
        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        }
    };

    private final CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
        @Override
//        攝像頭打開激發該方法
        public void onOpened(CameraDevice camera) {

            Log.e(TAG, "onOpened");
            cameraDevice = camera;
//            開始預覽
            createCameraPreview();
        }
//        攝像頭斷開連接時的方法
        @Override
        public void onDisconnected(CameraDevice camera) {
            cameraDevice.close();
            Main.this.cameraDevice = null;
        }
//        打開攝像頭出現錯誤時激發方法
        @Override
        public void onError(CameraDevice camera, int error) {
            cameraDevice.close();
            Main.this.cameraDevice = null;
        }
    };

    protected void startBackgroundThread() {
        mBackgroundThread = new HandlerThread("Camera Background");
        mBackgroundThread.start();
        mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
    }
    protected void stopBackgroundThread() {
        mBackgroundThread.quitSafely();
        try {
            mBackgroundThread.join();
            mBackgroundThread = null;
            mBackgroundHandler = null;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    protected void takePicture() {
        if(null == cameraDevice) {
            Log.e(TAG, "cameraDevice is null");
//            如果沒打開則返回
            return;
        }
//        攝像頭管理器,專門用於檢測、打開系統攝像頭,並連接CameraDevices.
        CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        try {
            Log.e(TAG,"cameraDevice.getId "+cameraDevice.getId());//查看選中的攝像頭ID
//            獲取指定攝像頭的相關特性(此處攝像頭已經打開)
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());

//            定義圖像尺寸
            Size[] jpegSizes = null;
            if (characteristics != null) {
//                獲取攝像頭支持的最大尺寸
                jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(ImageFormat.JPEG);
            }
            int width = 640;
            int height = 480;
            if (jpegSizes != null && 0 < jpegSizes.length) {
                width = jpegSizes[0].getWidth();
                height = jpegSizes[0].getHeight();
            }
//            創建一個ImageReader對象,用於獲得攝像頭的圖像數據
            ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
//動態數組
            List outputSurfaces = new ArrayList(2);

            outputSurfaces.add(reader.getSurface());
            outputSurfaces.add(new Surface(textureView.getSurfaceTexture()));
//生成請求對象(TEMPLATE_STILL_CAPTURE此處請求是拍照)
            final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
//            將ImageReader的surface作為captureBuilder的輸出目標
            captureBuilder.addTarget(reader.getSurface());
////設置自動對焦模式
//            captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
////            設置自動曝光模式
//            captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
            captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);//推薦采用這種最簡單的設置請求模式
            // 獲取設備方向
            int rotation = getWindowManager().getDefaultDisplay().getRotation();
//            根據設置方向設置照片顯示的方向
            captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));
//            設置圖片的存儲位置
            final File file = new File(Environment.getExternalStorageDirectory()+"/pic.jpg");
//ImageReader監聽函數
            ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
                @Override
                public void onImageAvailable(ImageReader reader) {
                    Image image = null;
//                    讀取圖像並保存
                    try {
                        image = reader.acquireLatestImage();
                        ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                        byte[] bytes = new byte[buffer.capacity()];
                        buffer.get(bytes);
                        save(bytes);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (image != null) {
                            image.close();
                        }
                    }
                }
                private void save(byte[] bytes) throws IOException {
                    OutputStream output = null;
                    try {
                        output = new FileOutputStream(file);
                        output.write(bytes);
                    } finally {
                        if (null != output) {
                            output.close();
                        }
                    }
                }
            };

            reader.setOnImageAvailableListener(readerListener, mBackgroundHandler);
            //拍照開始或是完成時調用,用來監聽CameraCaptureSession的創建過程
            final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
                @Override
//                拍照完成後提示圖片的保存位置
                public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                    super.onCaptureCompleted(session, request, result);
                    Toast.makeText(Main.this, "Saved:" + file, Toast.LENGTH_SHORT).show();
                    createCameraPreview();
                }
            };

            cameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {
                @Override
                public void onConfigured(CameraCaptureSession session) {
                    try {
                        session.capture(captureBuilder.build(), captureListener, mBackgroundHandler);
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    }
                }
                @Override
                public void onConfigureFailed(CameraCaptureSession session) {
                }
            }, mBackgroundHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }


    protected void createCameraPreview() {
        try {
            SurfaceTexture texture = textureView.getSurfaceTexture();
            assert texture != null;
//            設置默認的預覽大小
            texture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight());
            Surface surface = new Surface(texture);
//            請求預覽
            captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);

            captureRequestBuilder.addTarget(surface);
//            創建cameraCaptureSession,第一個參數是圖片集合,封裝了所有圖片surface,第二個參數用來監聽這處創建過程
            cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback(){
                @Override
                public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                    //The camera is already closed
                    if (null == cameraDevice) {
                        return;
                    }
                    // When the session is ready, we start displaying the preview.
                    cameraCaptureSessions = cameraCaptureSession;
                    updatePreview();
                }
                @Override
                public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
                    Toast.makeText(Main.this, "Configuration change", Toast.LENGTH_SHORT).show();
                }
            }, null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
    private void openCamera() {
//        實例化攝像頭
        CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        Log.e(TAG, "is camera open");
        try {
//            指定要打開的攝像頭
            cameraId = manager.getCameraIdList()[0];
//            獲取打開攝像頭的屬性
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
//The available stream configurations that this camera device supports; also includes the minimum frame durations and the stall durations for each format/size combination.
            StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            assert map != null;
            imageDimension = map.getOutputSizes(SurfaceTexture.class)[0];
            // Add permission for camera and let user grant the permission
//            權限檢查
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(Main.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CAMERA_PERMISSION);
                return;
            }
//            打開攝像頭,第一個參數代表要打開的攝像頭,第二個參數用於監測打開攝像頭的當前狀態,第三個參數表示執行callback的Handler,
//            如果程序希望在當前線程中執行callback,像下面的設置為null即可。
            manager.openCamera(cameraId, stateCallback, null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
        Log.e(TAG, "openCamera 1");
    }
    protected void updatePreview() {
        if(null == cameraDevice) {
            Log.e(TAG, "updatePreview error, return");
        }
//        設置模式為自動
        captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
        try {

            cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
    private void closeCamera() {
        if (null != cameraDevice) {
            cameraDevice.close();
            cameraDevice = null;
        }
        if (null != imageReader) {
            imageReader.close();
            imageReader = null;
        }
    }
//    如果沒有相應的權限測關閉APP
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CAMERA_PERMISSION) {
            if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
                // close the app
                Toast.makeText(Main.this, "Sorry!!!, you can't use this app without granting permission", Toast.LENGTH_LONG).show();
                finish();
            }
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume");
        startBackgroundThread();
        if (textureView.isAvailable()) {
            Log.e(TAG, "textureViewAvailable");
//            openCamera();
            takePicture();
        } else {
            textureView.setSurfaceTextureListener(textureListener);
        }
    }
    @Override
    protected void onPause() {
        Log.e(TAG, "onPause");
        closeCamera();
        stopBackgroundThread();
        super.onPause();
    }
}




//        takePictureButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                takePicture();
//            }
//        });

整個工程或是項目源碼下載地址:githubCamera2

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved