How to change the camera front and back and back to the click button in android

I am working on an Android tablet app. I want to change the camera from front to back and back to the button. How can i achieve this? I tried a few examples but did not get the correct answer.

I also add code.

public class PhotoPreview extends Activity implements SurfaceHolder.Callback { private Camera camera; private ImageButton cameraClick; private ImageButton cameraSwap; SurfaceView surfaceView; private SurfaceHolder mHolder; boolean previewing = false; String path = ""; LayoutInflater controlInflater = null; Bitmap bmp; Button cameraCancel; private SharedPreferences myPrefs; private int camId; /** Called when the activity is first created. */ @SuppressWarnings("deprecation") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); System.out.println("Photo preview Called $$$$$$$$$$$$$$$$$$ "); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.photo_preview); myPrefs = this.getSharedPreferences("myPrefs", MODE_PRIVATE); camId = myPrefs.getInt("camId",1); System.out.println("CAM ID $$$$$$$$$$$$$$$$$$ "+camId); getWindow().setFormat(PixelFormat.UNKNOWN); surfaceView = (SurfaceView) findViewById(R.id.camerapreview); mHolder = surfaceView.getHolder(); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mHolder.addCallback(this); controlInflater = LayoutInflater.from(getBaseContext()); View viewControl = controlInflater.inflate(R.layout.control, null); LayoutParams layoutParamsControl = new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); this.addContentView(viewControl, layoutParamsControl); cameraClick = (ImageButton) findViewById(R.id.cameraClick); cameraClick.setOnClickListener(cameraClickListener); cameraCancel = (Button) findViewById(R.id.cameraCancel); cameraCancel.setOnClickListener(cameraCancelClickListener); cameraSwap = (ImageButton) findViewById(R.id.cameraSwap); cameraSwap.setOnClickListener(swapCameraClickListener); } /* * This is click event of Photo capture button */ private OnClickListener cameraClickListener = new OnClickListener() { @Override public void onClick(final View v) { if (camera != null) { camera.takePicture(shutterCallback, rawCallback, jpegCallback); } } }; /* * This is click event of camera cancel button */ private OnClickListener cameraCancelClickListener = new OnClickListener() { @Override public void onClick(final View v) { Intent intent = new Intent(PhotoPreview.this, MainScreenActivity.class); startActivity(intent); } }; /* * This is click event of camera cancel button */ private OnClickListener swapCameraClickListener = new OnClickListener() { @Override public void onClick(final View v) { if (camId == 0) { SharedPreferences.Editor prefsEditor = myPrefs.edit(); prefsEditor.putInt("camId", 1); prefsEditor.commit(); } else { SharedPreferences.Editor prefsEditor = myPrefs.edit(); prefsEditor.putInt("camId", 0); prefsEditor.commit(); } System.out.println("CAM ID ^^^^^^^^^^^^^^^^^^^^ "+camId); Intent intent = new Intent(PhotoPreview.this, PhotoPreview.class); startActivity(intent); } }; // Handles when shutter open ShutterCallback shutterCallback = new ShutterCallback() { public void onShutter() { } }; /** Handles data for raw picture */ PictureCallback rawCallback = new PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { } }; /** Handles data for jpeg picture */ PictureCallback jpegCallback = new PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { bmp = BitmapFactory.decodeByteArray(data, 0, data.length); String root = Environment.getExternalStorageDirectory().toString(); File myDir = new File(root + "/Easy_Measurement_images"); myDir.mkdirs(); Random generator = new Random(); int n = 10000; n = generator.nextInt(n); String fname = "Image-" + n + ".jpg"; File file = new File(myDir, fname); if (file.exists()) file.delete(); try { FileOutputStream out = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.PNG, 90, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } Intent intent = new Intent(PhotoPreview.this, VerticalAdjustmentActivity.class); startActivity(intent); } }; @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // Set camera preview size,orientation,rotation using parameters if (camera != null) { Camera.Parameters parameters = camera.getParameters(); parameters.set("orientation", "portrait"); camera.setParameters(parameters); camera.startPreview(); } } @Override public void surfaceCreated(SurfaceHolder holder) { System.out.println("CAM ID %%%%%%%%%%%%%%$$$$$$$$$$$$$$$$$$ "+camId); camera = Camera.open(camId); if (camera != null) { try { camera.setPreviewDisplay(holder); } catch (IOException e) { e.printStackTrace(); } } } @Override public void surfaceDestroyed(SurfaceHolder holder) { System.out.println("Surface destroyed ***************"); if (camera != null) { camera.stopPreview(); camera.release(); camera = null; } } 

}

+4
source share
1 answer

you can open the camera using Camera.open (int i) . You can use only one camera at a time. So release one, and then open the other. for instance

 public void onClick(View v) { camera.stopPreview(); camera.release(); if(currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK){ currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT; } else { currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK; } camera = Camera.open(currentCameraId); setCameraDisplayOrientation(CameraActivity.this, currentCameraId, camera); try { camera.setPreviewDisplay(previewHolder); } catch (IOException e) { e.printStackTrace(); } camera.startPreview(); 

}

0
source

Source: https://habr.com/ru/post/1479134/


All Articles