How to add a rectangular overlay in a camera application?

前端 未结 2 727
悲哀的现实
悲哀的现实 2021-01-31 06:44

How do I add a rectangular overlay (which should be more like a rectangular frame) in the camera preview? My application consists of a button that opens the camera upon clicking

相关标签:
2条回答
  • 2021-01-31 07:06

    You have to create your own Preview by extending the SurfaceView class.

    See below link which helps you.

    Custom camera android

    Take FrameLayout with SurfaceView as child.and customise as per your needs

    0 讨论(0)
  • 2021-01-31 07:16

    First, create a public Class that extends View. Inside its onDraw() method, draw your rectangle. For example:

    public class Box extends View {
      private Paint paint = new Paint();
      Box(Context context) {
        super(context);
      }
    
      @Override
      protected void onDraw(Canvas canvas) { // Override the onDraw() Method
        super.onDraw(canvas);
    
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(10);
    
        //center
        int x0 = canvas.getWidth()/2;
        int y0 = canvas.getHeight()/2;
        int dx = canvas.getHeight()/3;
        int dy = canvas.getHeight()/3;
        //draw guide box
        canvas.drawRect(x0-dx, y0-dy, x0+dx, y0+dy, paint);
      }
    }
    

    Then, in your camera preview Activity, have an instance to your Box class:

    Box box = new Box(this);
    

    Finally:

    addContentView(box, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    

    A green rectangle will be drawn onto your camera preview.

    0 讨论(0)
提交回复
热议问题