??????上一篇文章Android 自定義相機 識別身份證(上)介紹自定義相機的一些相關(guān)技術(shù)和步驟,這篇文章主要把代碼簡單的介紹一下.具體可以去看我源碼,已分享到GitHub上去了.
自定義SurfaceView
??????通過自定義SurfaceView來實現(xiàn)預覽界面,并實現(xiàn)了SurfaceHolder.Callback這個接口,因此我們可以把相機實例化、預覽和釋放相機的方法都放在這這個自定義SurfaceView里面,這樣在頁面中只需要調(diào)用一個拍照方法就可以了.
??????首先是重寫的三個方法:
public void surfaceCreated(SurfaceHolder holder) {
mCamera = CamParaUtil.openCamera();
if (mCamera != null) {
startPreview(holder);
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
//不加這句的話相機拍照返回出現(xiàn)卡死的情況
mCamera.stopPreview();
startPreview(holder);
}
public void surfaceDestroyed(SurfaceHolder holder) {
//回收釋放資源
release();
}
??????在startPreview()中我們需要設置預覽角度和獲取拍攝的最佳大小,具體原因在上篇文章中都有說明.
private void startPreview( SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
Camera.Parameters parameters = mCamera.getParameters();
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
//豎屏拍照時,需要設置旋轉(zhuǎn)90度,否者看到的相機預覽方向和界面方向不相同
mCamera.setDisplayOrientation(90);
parameters.setRotation(90);
} else {
mCamera.setDisplayOrientation(0);
parameters.setRotation(0);
}
Camera.Size bestSize = CamParaUtil.getBestSize(parameters.getSupportedPreviewSizes());
if (bestSize != null) {
parameters.setPreviewSize(bestSize.width, bestSize.height);
parameters.setPictureSize(bestSize.width, bestSize.height);
} else {
parameters.setPreviewSize(1920, 1080);
parameters.setPictureSize(1920, 1080);
}
mCamera.setParameters(parameters);
mCamera.startPreview();
focus();
} catch (Exception e) {
}
}
??????剩下的就是自動聚焦和釋放相機資源,因為代碼比較簡單,這里就不在粘貼.
拍照返回
??????在Activity 調(diào)用拍照方法,拍照完成之后,會對照片按比例做裁剪,實現(xiàn)預覽多大就拍出多大的照片,把照片存儲到本地并把存儲路徑回傳給頁面,這樣就能顯示出拍出的照片.
private void takePhoto() {
optionView.setVisibility(View.GONE);
customCameraPreview.setEnabled(false);
customCameraPreview.takePhoto(new Camera.PictureCallback() {
public void onPictureTaken(final byte[] data, final Camera camera) {
//子線程處理圖片,防止ANR
new Thread(new Runnable() {
@Override
public void run() {
Bitmap bitmap = null;
if (data != null) {
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
camera.stopPreview();
}
if (bitmap != null) {
//計算裁剪位置
float left = ((float) containerView.getLeft() - (float) customCameraPreview.getLeft()) / (float) customCameraPreview.getWidth();
float top = (float) cropView.getTop() / (float) customCameraPreview.getHeight();
float right = (float) containerView.getRight() / (float) customCameraPreview.getWidth();
float bottom = (float) cropView.getBottom() / (float) customCameraPreview.getHeight();
//裁剪及保存到文件
Bitmap resBitmap = Bitmap.createBitmap(bitmap,
(int) (left * (float) bitmap.getWidth()),
(int) (top * (float) bitmap.getHeight()),
(int) ((right - left) * (float) bitmap.getWidth()),
(int) ((bottom - top) * (float) bitmap.getHeight()));
FileUtil.saveBitmap(resBitmap);
if (!bitmap.isRecycled()) {
bitmap.recycle();
}
if (!resBitmap.isRecycled()) {
resBitmap.recycle();
}
//拍照完成,返回對應圖片路徑
Intent intent = new Intent();
intent.putExtra("result", FileUtil.getImgPath());
setResult(RESULT_OK, intent);
finish();
}
return;
}
}).start();
}
});
}
保存文件
??????剩下的就是保存照片,并返回路徑.
/**
* 保存Bitmap到sdcard
*
* @param b 得到的圖片
*/
public static void saveBitmap(Bitmap b) {
String path = initPath();
long dataTake = System.currentTimeMillis();
imgPath = path + "/" + dataTake + ".jpg";
try {
FileOutputStream fout = new FileOutputStream(imgPath);
BufferedOutputStream bos = new BufferedOutputStream(fout);
b.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @return 保存到sd卡的圖片路徑
*/
public static String getImgPath() {
return imgPath;
}