您好,登錄后才能下訂單哦!
我們知道自定義Camera需要以下幾步
1、打開相機
public static Camera openFacingBackCamera() { Camera cam = null; Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); for (int camIdx = 0, cameraCount = Camera.getNumberOfCameras(); camIdx < cameraCount; camIdx++) { Camera.getCameraInfo(camIdx, cameraInfo); if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { try { //打開相應的攝像頭,目前是前置攝像頭 cam = Camera.open(camIdx); } catch (RuntimeException e) { e.printStackTrace(); if (cam!=null){ cam.stopPreview(); cam.release(); } cam = null; } } } return cam; }
2、設置相關參數和預覽
//定義SurfaceView private SurfaceView localSurface; // 為surfaceHolder添加一個回調監聽器 localSurface.getHolder().addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { camera = CameraUtil.openFacingBackCamera(); //設置相機預覽旋轉角度 camera.setDisplayOrientation(180); if (camera != null) { Camera.Parameters parameters = camera.getParameters();// 獲得相機參數 List<Camera.Size> sizes = parameters.getSupportedPreviewSizes(); List<Camera.Size> sizes_pic = parameters.getSupportedPictureSizes(); Camera.Size cs = sizes.get(0); int mWidth = cs.width; int mHeight = cs.height; parameters.setPreviewSize(mWidth, mHeight); parameters.setPictureSize(sizes_pic.get(0).width, sizes_pic.get(0).height); parameters.setJpegQuality(85);// parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); parameters.setPictureFormat(ImageFormat.JPEG); camera.setParameters(parameters); try { camera.setPreviewDisplay(localSurface.getHolder()); camera.startPreview(); // 開始預覽 camera.autoFocus(null); // 自動對焦 } catch (IOException e) { e.printStackTrace(); camera.stopPreview(); camera.release(); camera = null; } } } @Override public void surfaceDestroyed(SurfaceHolder holder) { if (camera!=null){ camera.stopPreview(); camera.release(); camera = null; } } } ); // 設置該SurfaceView自己不維護緩沖 localSurface.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
3、獲取圖片
private void getViewImage() { //設置監聽 camera.setPreviewCallback(new Camera.PreviewCallback(){ @Override public void onPreviewFrame(byte[] data, Camera camera) { Camera.Size size = camera.getParameters().getPreviewSize(); try{ YuvImage image = new YuvImage(data, ImageFormat.NV21, size.width, size.height, null); if(image!=null){ ByteArrayOutputStream stream = new ByteArrayOutputStream(); image.compressToJpeg(new Rect(0, 0, size.width, size.height), 80, stream); Bitmap bmp = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size()); //因為圖片會放生旋轉,因此要對圖片進行旋轉到和手機在一個方向上 rotateBitmap(bmp); stream.close(); } }catch(Exception ex){ Log.e("Sys","Error:"+ex.getMessage()); } } }); }
旋轉圖片的方法
public void rotateMyBitmap(Bitmap bmp){ Matrix matrix = new Matrix(); matrix.postScale(-1, 1); // 鏡像水平翻轉(如果左右顛倒) matrix.postRotate(180); Bitmap nbmp = Bitmap.createBitmap(bmp, 0,0, bmp.getWidth(), bmp.getHeight(), matrix, true); imageView.setImageBitmap(nbmp); };
自定義時間拍照
private Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 101: getViewImage(); Log.e(TAG, "handleMessage: 拍照" ); mHandler.sendEmptyMessageDelayed(102, 100); break; case 102: camera.setPreviewCallback(null); break ; } } };
然后點擊按鈕調用
mHandler.sendEmptyMessageDelayed(101, 100);
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。