4

Actually I don't understand how to implement the rectangle on the camera preview. Everything working well, but I don't know the way to put the rectangle border like this link. Please anyone help me. Thanks.

Paul
  • 179
  • 1
  • 8
Miss A
  • 49
  • 1
  • 1
  • 4
  • depending on how and where are you showing the camera preview, just add a imageview over the layout that shows the preview, with a .png image that contains the square. – CptEric Aug 04 '15 at 07:29

1 Answers1

3

I suggest you implement your camera preview in a custom SurfaceView and then in your XML you can simply overlay the rectangle, like the accepted answer here:

Android: Overlay on Android Camera Preview

So add a Java file to your project:

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();
    }

    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);
    }
}

Please note you will have to make sure it has the appropriate package name.

Then change your main XML file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <[PACKAGE_NAME].CapturePreview 
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>


  <LinearLayout 
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background:"#55999999"
      android:padding:"30dp"
      android:gravity="center">

  </LinearLayout>  

</RelativeLayout>

This should have a transparent rectangle over the camera preview.

Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95
  • 1
    The guidelines of the community do entail that you post what you have tried. Everyone is trying to accommodate you, but you need to show some of the work you have done in order for us to help you effectively. What is it do you not understand? Have you tried to implement the CapturePreview? Can you add it in the XML? – Quintin Balsdon Aug 04 '15 at 12:12
  • @Karandeep Atwal - thank you for your criticism, however you will see I have a LinearLayout overlaying CamperPreview. The question is asking for a rectangle, and while I could override the onDraw() method and access a canvas like that, it complicates an already complicated class. Not that a canvas was a requirement in the first place. You are more than welcome to attempt to answer the question to "Miss A"'s satisfaction yourself. – Quintin Balsdon Apr 24 '17 at 13:04