Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use SurfaceView and create a CustomView that will open the camera and you can adjust its size in the xml accordingly. Below is a pseudo code.</p> <p><strong>Create a class that extends SurfaceView and open camera inside that</strong></p> <p><strong>CapturePreview.java</strong> </p> <pre><code>public class CapturePreview extends SurfaceView implements SurfaceHolder.Callback{ public static Bitmap mBitmap; SurfaceHolder holder; static Camera mCamera; public CapturePreview(Context context, AttributeSet attrs) { super(context, attrs); holder = getHolder(); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) { Camera.Parameters parameters = mCamera.getParameters(); parameters.getSupportedPreviewSizes(); mCamera.setParameters(parameters); mCamera.startPreview(); } @Override public void surfaceCreated(SurfaceHolder holder) { try { mCamera = Camera.open(); mCamera.setPreviewDisplay(holder); } catch (Exception e) { e.printStackTrace(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { mCamera.stopPreview(); mCamera.release(); } /*** * * Take a picture and and convert it from bytes[] to Bitmap. * */ public static void takeAPicture(){ Camera.PictureCallback mPictureCallback = new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { BitmapFactory.Options options = new BitmapFactory.Options(); mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options); } }; mCamera.takePicture(null, null, mPictureCallback); } } </code></pre> <p><strong>Now you have to include the view that you created using SurfaceView in your xml like this</strong></p> <p><strong>main.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;FrameLayout android:id="@+id/mySurfaceView" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt; &lt;com.cam.CapturePreview android:layout_width="fill_parent" android:layout_height="wrap_content"&gt; &lt;/com.cam.CapturePreview&gt; &lt;/FrameLayout&gt; &lt;LinearLayout android:layout_below="@id/mySurfaceView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center"&gt; &lt;ImageView android:id="@+id/myImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon"/&gt; &lt;/LinearLayout&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Now you can use this main.xml in any <code>Acitivty</code> that will open a camera with a <code>ImageView</code>. Thanks....</p>
 

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