Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to switch from front and back cameras on a button click?
    primarykey
    data
    text
    <p>i want to switch between the front and back cameras on a button click .</p> <p>when any one of the camera's is open , i need to release it and open the other one .</p> <p>could anyone tell me the block of code to switch ?</p> <p>Thank you in advance . . . . </p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:background="@drawable/camera_btn"/&gt; &lt;Button android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:background="@drawable/front_back_btn"/&gt; &lt;ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/Button01" android:contentDescription="@string/app_name"&gt; &lt;/ImageView&gt; &lt;com.example.surfacecamera.CameraView android:id="@+id/CameraView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>THIS IS MY ACTIVITY </p> <p>and main java class is </p> <pre><code>package com.example.surfacecamera; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Bitmap.CompressFormat; import android.hardware.Camera; public class MainActivity extends Activity implements OnClickListener, Camera.PictureCallback { CameraView cameraView; ImageView imv; Button b2 ; int cameraCount = 0; int camIdx = 0; Camera cam; Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); setContentView(R.layout.activity_main); Button pictureButton = (Button) this.findViewById(R.id.Button01); Button b2 = (Button) this.findViewById(R.id.Button02); imv = (ImageView) this.findViewById(R.id.ImageView01); cameraView = (CameraView) this.findViewById(R.id.CameraView01); cameraCount = Camera.getNumberOfCameras(); b2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { } }); pictureButton.setOnClickListener(this); } // From the OnClickListener public void onClick(View v) { cameraView.takePicture(null, null, this); } // From the Camera.PictureCallback public void onPictureTaken(byte[] data, Camera camera) { Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length); imv.setImageBitmap(bmp); String filename = "apicture.jpg"; File pictureFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + filename); try { FileOutputStream pfos = new FileOutputStream(pictureFile); bmp.compress(CompressFormat.JPEG, 75, pfos); pfos.flush(); pfos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } </code></pre> <p>and another class</p> <pre><code>package com.example.surfacecamera; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import android.content.Context; import android.content.res.Configuration; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.util.AttributeSet; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; public class CameraView extends SurfaceView implements SurfaceHolder.Callback { SurfaceHolder mHolder; int width; int height; Camera mCamera; public CameraView(Context context, AttributeSet attrs) { super(context,attrs); holderCreation(); } public CameraView(Context context) { super(context); holderCreation(); } public void takePicture(Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback jpeg) { mCamera.takePicture(shutter, raw, jpeg); } @SuppressWarnings("deprecation") public void holderCreation() { // Install a SurfaceHolder.Callback so we get notified when the // underlying surface is created and destroyed. mHolder = getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } public void surfaceCreated(SurfaceHolder holder) { // The Surface has been created, acquire the camera and tell it where to draw. mCamera = Camera.open(); Parameters params = mCamera.getParameters(); // If we aren't landscape (the default), tell the camera we want portrait mode if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { params.set("orientation", "portrait"); // "landscape" // And Rotate the final picture if possible // This works on 2.0 and higher only //params.setRotation(90); // Use reflection to see if it exists and to call it so you can support older versions try { Method rotateSet = Camera.Parameters.class.getMethod( "setRotation", new Class[] { Integer.TYPE } ); Object arguments[] = new Object[] { new Integer(90) }; rotateSet.invoke(params, arguments); } catch (NoSuchMethodException nsme) { // Older Device Log.v("CameraView","No Set Rotation"); } catch (IllegalArgumentException e) { Log.v("CameraView","Exception IllegalArgument"); } catch (IllegalAccessException e) { Log.v("CameraView","Illegal Access Exception"); } catch (InvocationTargetException e) { Log.v("CameraView","Invocation Target Exception"); } } mCamera.setParameters(params); try { mCamera.setPreviewDisplay(holder); } catch (IOException exception) { mCamera.release(); mCamera = null; } } public void surfaceDestroyed(SurfaceHolder holder) { // Surface will be destroyed when we return, so stop the preview. // Because the CameraDevice object is not a shared resource, it's very // important to release it when the activity is paused. mCamera.stopPreview(); mCamera.release(); mCamera = null; } public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { width = w; height = h; // Now that the size is known, set up the camera parameters and begin the preview. Camera.Parameters parameters = mCamera.getParameters(); //parameters.setPreviewSize(w, h); mCamera.setParameters(parameters); mCamera.startPreview(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload