編輯:關於Android編程
Camera
take picture
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();
// }
// });
本文實例講述了Android編程實現自定義系統菜單背景的方法。分享給大家供大家參考,具體如下:不多說,上圖,見代碼。package lab.sodino.menutest
在前一篇文章中,我主要講解了Android源碼中的Touch事件的傳遞過程,現在我想使用一個demo以及一個實例來學習一下Andorid中的T
ListView是Android中經常會使用的東西,綁定數據對於初學者來說,尤其是剛接觸編程的人來說,往往會覺得很難理解,我上大二的時候學的java,但是基本上相當於沒有
Android基礎入門教程——2.4.5 ListView之checkbox錯位問題解決標簽(空格分隔): Android基礎入門教程本節引言: