This article shares with you the functions of Android to switch cameras, take photos and save them to albums, previews, etc., to solve the problems of not being able to preview continuously after taking photos from Android 7, parameter settings related to the inversion of front and rear camera languages.
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import .; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class CamaraActivity extends Activity implements { @Bind(.return_back) TextView returnBack; @Bind(.tvswitch_camera) TextView tvswitchCamera; //Start the camera private Camera mCamera; public boolean isopen_camara = false; private SurfaceView surfaceView; private SurfaceHolder mholder = null; private SurfaceCallback previewCallBack; private boolean isTakingPhoto;//Is it taking pictures @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); verifyPermission(new String[]{}); // getWindow().setFlags(.FLAG_FULLSCREEN, .FLAG_FULLSCREEN); setContentView(.activity_camara); (this); // Preview control surfaceView = (SurfaceView) this .findViewById(); // Set parameters ().setKeepScreenOn(true); ().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); (this); (this); ().register(this); } public void verifyPermission(String[] permissions) { if (permissions != null) { List<String> lists = new ArrayList<>(); for (int i = 0; i < ; i++) { if ((this, permissions[i]) != PackageManager.PERMISSION_GRANTED) { if ((this, permissions[i])) { } (permissions[i]); } } if (() > 0) { String[] ps = new String[()]; for (int i = 0; i < (); i++) { ps[i] = (i); } (this, ps, 1); } } } @Override protected void onRestart() { (); } @Override protected void onStart() { (); } private void takePhoto() { if (!isopen_camara) { previewCallBack = new SurfaceCallback(); ().addCallback(previewCallBack); } else { autoTakePhoto(); } } @Override protected void onResume() { (); if(mCamera == null){ if (!isopen_camara) { previewCallBack = new SurfaceCallback(); ().addCallback(previewCallBack); } } } @Override public void onClick(View v) { switch (()) { case .return_back: finish(); break; case .tvswitch_camera: switchCamara(); break; } } // Preview interface callback private final class SurfaceCallback implements { // The preview interface is created public void surfaceCreated(SurfaceHolder holder) { try { //1 means to turn on the rear camera, and 0 means to turn on the front camera. mCamera = (cameraPosition);// Turn on the camera setParams(holder,cameraPosition); } catch (Exception e) { (); if (mCamera != null) { (); (); (); mCamera = null; } finish(); //(getApplicationContext(), "This phone does not support the automatic photo shooting function", Toast.LENGTH_LONG).show(); } } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { ("surfaceChanged"); isopen_camara = true; //autoTakePhoto(); } // The preview interface is destroyed public void surfaceDestroyed(SurfaceHolder holder) { ("surfaceDestroyed"); if(!isopen_camara) return; if (mCamera != null) { (this); (null); (); (); (); mCamera = null; } } } public void reset(){ cameraInfo = new (); if (mCamera != null) { if(surfaceView != null && () != null && previewCallBack != null){ ().removeCallback(previewCallBack); } (null); (); (); (); mCamera = null; mCamera = (cameraPosition); if (null != mholder) setParams(mholder,); } } @Override protected void onDestroy() { (); if (mCamera != null) { if(surfaceView != null && () != null && previewCallBack != null){ ().removeCallback(previewCallBack); } (null); (); (); (); mCamera = null; } (this); ().unregister(this); } private void setParams(SurfaceHolder mySurfaceView,int postion) { try { int PreviewWidth = 0; int PreviewHeight = 0; WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);//Get the window manager /*PreviewWidth = display.(); PreviewHeight = ();*/ parameters = (); // Select the appropriate preview size List<> sizeList = (); // If there is only one sizeList, we don’t need to do anything, because only he has no choice but to do nothing, because only he has no choice if (() > 1) { Iterator<> itor = (); while (()) { cur = (); /*if ( >= PreviewWidth && >= PreviewHeight) {*/ if ( >= PreviewWidth && >= PreviewHeight) { PreviewWidth = ; PreviewHeight = ; break; } } } (PreviewWidth, PreviewHeight); //Get the size of the camera area //(3);//3 frames per second Get 3 pictures from the camera per second //(3,); List<int[]> list = (); int[] v = null; int index = 0; int min = 0; for (int i = 0; i < (); i++) { v = (i); if (v[0] > min) { min = v[0]; index = i; } } ((index)[0], (index)[1]); ();//Set the format of photo output ("jpeg-quality", 85);//Set photo quality (PreviewWidth, PreviewHeight);//Set the screen size (180); //Java part if (.SDK_INT >= 24) { if(postion == 0){ (90); }else { (270); } } else { (90); } (parameters);// Assign the above settings to the camera (mySurfaceView);//Show the camera to get the screen in the SurfaceView control mholder = mySurfaceView; (new () { @Override public void onPreviewFrame(byte[] data, Camera camera) { } }); ();//Start preview // mPreviewRunning = true; } catch (IOException e) { ("tag", ()); } } Handler handler = null; private void autoTakePhoto() { // Focus is required before taking a photo. Get the clear picture if (null == mCamera) return; (new () { @Override public void onAutoFocus(boolean success, Camera camera) { if (success && isopen_camara) { // Focus successfully // (, "Focus successfully!!",Toast.LENGTH_SHORT).show(); if(!isTakingPhoto) { isTakingPhoto = true; handler = new Handler(); (new Runnable() { @Override public void run() { (null, null, new MyPictureCallback()); } }); } } } }); } // Photo callback private final class MyPictureCallback implements { // After the photo is generated @Override public void onPictureTaken(byte[] data, Camera camera) { try { Bitmap bitmap = (data, 0, ); Matrix matrix = new Matrix(); (270); File jpgFile = new File(() + "/DCIM/camera"); if (!()) { (); } File jpgFile1 = new File((), () + ".jpg"); bitmap = (bitmap, 0, 0, (), (), matrix, true); FileOutputStream fos = new FileOutputStream(jpgFile1); (, 80, fos); (getApplicationContext(), getString(.save_success)); (); Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = (jpgFile1); (uri); sendBroadcast(intent); } catch (Exception e) { (); }finally { if(.SDK_INT>=24){ reset(); } isTakingPhoto = false; } } } private int cameraPosition = 1;//0 represents the front camera, 1 represents the rear camera private void switchCamara() { //Switch front and rear cameras int cameraCount = 0; cameraInfo = new (); cameraCount = ();//Get the number of cameras for (int i = 0; i < cameraCount; i++) { (i, cameraInfo);//Get information about each camera if (cameraPosition == 1) { //It is now rear, changed to front if ( == .CAMERA_FACING_BACK) {//Represents the orientation of the camera, CAMERA_FACING_FRONT front CAMERA_FACING_BACK rear CAMERA_FACING_BACK rear if(surfaceView != null && () != null && previewCallBack != null){ ().removeCallback(previewCallBack); } (null); ();//Stop the preview of the original camera (); ();//Release resources mCamera = null;//Cancel the original camera mCamera = (i);//Open the currently selected camera /* try { if (null != mholder) (mholder);//Show the framing screen through surfaceview } catch (IOException e) { // TODO Auto-generated catch block (); } ();//Start preview*/ if (null != mholder) setParams(mholder,.CAMERA_FACING_BACK); cameraPosition = 0; break; } } else { //Now it is the front, changed to the rear if ( == .CAMERA_FACING_FRONT) {//Represents the orientation of the camera, CAMERA_FACING_FRONT front CAMERA_FACING_BACK rear CAMERA_FACING_BACK rear if(surfaceView != null && () != null && previewCallBack != null){ ().removeCallback(previewCallBack); } (null); ();//Stop the preview of the original camera (); ();//Release resources mCamera = null;//Cancel the original camera mCamera = (i);//Open the currently selected camera /*try { if (null != mholder) (mholder);//Show the framing screen through surfaceview } catch (IOException e) { // TODO Auto-generated catch block (); } ();//Start preview*/ if (null != mholder) setParams(mholder,.CAMERA_FACING_FRONT); cameraPosition = 1; break; } } } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {///Pull volume down to take photos takePhoto(); return true; } return (keyCode, event); } }
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.