如何使用Google的camera2自动捕获叠加层中没有任何用户界面(“按钮”)的内容?

时间:2018-11-14 08:58:56

标签: android android-camera2

我正在使用google camera2 API,并能够使用表面视图成功制作绿色矩形叠加层。现在的挑战是,仅当对象在覆盖叠加层的四个角之后进入矩形叠加层内时,才能使用无按钮捕获预览。尝试了很多,但仍然没有成功。整个代码来自google示例camera2,因此仅显示进行更改的部分。 (https://github.com/googlesamples/android-Camera2Basic):

 @Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
    view.findViewById(R.id.picture).setOnClickListener(this);
    view.findViewById(R.id.info).setOnClickListener(this);
    mTextureView =  view.findViewById(R.id.texture);

    final SurfaceView surfaceView =  view.findViewById(R.id.surfaceView);
    surfaceView.setVisibility(View.VISIBLE);
    surfaceView.setZOrderOnTop(true);
    SurfaceHolder mHolder = surfaceView.getHolder();


    mHolder.setFormat(PixelFormat.TRANSPARENT);
    mHolder.addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder)
        {
            Canvas canvas = holder.lockCanvas();
            if (canvas == null) {
                Log.e(TAG, "Cannot draw onto the canvas as it's null");
            } else {
                int w = canvas.getWidth();
                int h = canvas.getHeight();
                int outerFillColor = 0x33000000;
                float radius = 10.0f;

                RectF rect = new RectF(100, 100, w - 100, h - 100);
// first create an off-screen bitmap and its canvas
                Bitmap bitmap = null;
                if (android.os.Build.VERSION.SDK_INT >= 
android.os.Build.VERSION_CODES.O) {
                    bitmap = Bitmap.createBitmap(w, h, 
Bitmap.Config.ARGB_8888);
                }
                Canvas auxCanvas = new Canvas(bitmap);

// then fill the bitmap with the desired outside color

                Paint paint = new Paint(Paint.FAKE_BOLD_TEXT_FLAG);
                paint.setColor(outerFillColor);
                paint.setStyle(Paint.Style.FILL);
                auxCanvas.drawPaint(paint);
// then punch a transparent hole in the shape of the rect
                paint.setXfermode(new 
 PorterDuffXfermode(PorterDuff.Mode.CLEAR));
                auxCanvas.drawRoundRect(rect, radius, radius, paint);

// then draw the white rect border (being sure to get rid of the xfer 
 mode!)
                paint.setXfermode(null);
                paint.setColor(Color.GREEN);
                paint.setStyle(Paint.Style.STROKE);
                auxCanvas.drawRoundRect(rect, radius, radius, paint);


// finally, draw the whole thing to the original canvas
                canvas.drawBitmap(bitmap, 0, 0, paint);
                holder.unlockCanvasAndPost(canvas);

            }

        }

1 个答案:

答案 0 :(得分:0)

检查此链接。 本节中的图像捕获onClick事件。通过将捕获代码放置在所需的位置,您无需单击即可捕获图像

  

Show rectangle in camera preview and crop image within it using android camera2