We know that custom Camera requires the following steps
- Turn on the camera, that is, instantiate the Camera object, Camera camera = ();
- Set the relevant parameters of Camera, parameters = ();
- Open preview, (surfaceholder); ();
- Get the picture, here is just getting it from the preview so it is used, (new (){…..});
- Stop preview, release the camera, ();();
1. Turn on the camera
public static Camera openFacingBackCamera() { Camera cam = null; cameraInfo = new (); for (int camIdx = 0, cameraCount = (); camIdx < cameraCount; camIdx++) { (camIdx, cameraInfo); if ( == .CAMERA_FACING_FRONT) { try { //Open the corresponding camera, it is currently the front camera cam = (camIdx); } catch (RuntimeException e) { (); if (cam!=null){ (); (); } cam = null; } } } return cam; }
2. Set relevant parameters and previews
//Define SurfaceViewprivate SurfaceView localSurface; // Add a callback listener to surfaceHolder ().addCallback(new () { @Override public void surfaceCreated(SurfaceHolder holder) { } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { camera = (); //Set the camera preview rotation angle (180); if (camera != null) { parameters = ();// Obtain camera parameters List<> sizes = (); List<> sizes_pic = (); cs = (0); int mWidth = ; int mHeight = ; (mWidth, mHeight); (sizes_pic.get(0).width, sizes_pic.get(0).height); (85);// (.FOCUS_MODE_AUTO); (); (parameters); try { (()); (); // Start preview (null); // Autofocus } catch (IOException e) { (); (); (); camera = null; } } } @Override public void surfaceDestroyed(SurfaceHolder holder) { if (camera!=null){ (); (); camera = null; } } } ); // Set the SurfaceView itself to not maintain buffering ().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
3. Get pictures
private void getViewImage() { //Set up the monitoring (new (){ @Override public void onPreviewFrame(byte[] data, Camera camera) { size = ().getPreviewSize(); try{ YuvImage image = new YuvImage(data, ImageFormat.NV21, , , null); if(image!=null){ ByteArrayOutputStream stream = new ByteArrayOutputStream(); (new Rect(0, 0, , ), 80, stream); Bitmap bmp = ((), 0, ()); //Because the picture will be released and rotated, the picture should be rotated in the same direction as the phone rotateBitmap(bmp); (); } }catch(Exception ex){ ("Sys","Error:"+()); } } }); }
How to rotate pictures
public void rotateMyBitmap(Bitmap bmp){ Matrix matrix = new Matrix(); (-1, 1); // The mirror is flipped horizontally (if left and right is reversed) (180); Bitmap nbmp = (bmp, 0,0, (), (), matrix, true); (nbmp); };
Custom time to take photos
private Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch () { case 101: getViewImage(); (TAG, "handleMessage: Take a photo" ); (102, 100); break; case 102: (null); break ; } } };
Then click the button to call
(101, 100);
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.