只拍摄SurfaceView预览界面特定区域内容(矩形框)
只拍摄SurfaceView预览界面特定区域内容(矩形框)
Any_命运 发表于4年前
只拍摄SurfaceView预览界面特定区域内容(矩形框)
  • 发表于 4年前
  • 阅读 55
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

package com.comtop.mdl.barcode;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
//import android.graphics.Shader;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.View;

import com.comtop.mdl.R;
import com.google.zxing.ResultPoint;

/**
 * This view is overlaid on top of the camera preview. It adds the viewfinder
 * rectangle and partial transparency outside it, as well as the laser scanner
 * animation and result points.
 * 
 * @author dswitkin@google.com (Daniel Switkin)
 */
public final class ViewCameraView extends View {
        
//        private static final int[] SCANNER_ALPHA = { 0, 64, 128, 192, 255, 192,
//                        128, 64 };
//        
//        private static final long ANIMATION_DELAY = 100L;
        private static final int CURRENT_POINT_OPACITY = 0xA0;
        private static final int OPAQUE = 0xFF;
        private static final int POINT_SIZE = 6;
        private final Paint paint;
        private Bitmap resultBitmap;
        private final int maskColor;
        private final int resultColor;
        private final int laserColor;
        private final int resultPointColor;
        int scannerAlpha;
        private List<ResultPoint> possibleResultPoints;
        private List<ResultPoint> lastPossibleResultPoints;
        private boolean laserLinePortrait = true;
        Rect mRect;
        int i = 0;
        GradientDrawable mDrawable;
        Paint textPaint;

        // This constructor is used when the class is built from an XML resource.
        public ViewCameraView(Context context, AttributeSet attrs) {
                super(context, attrs);

                // Initialize these once for performance rather than calling them every
                // time in onDraw().
                paint = new Paint();
                textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
                mRect = new Rect();
                int left = Color.TRANSPARENT;
                int center = Color.GREEN;
                int right = Color.TRANSPARENT;//0xe000FF00
                //中间扫描线背景
                mDrawable = new GradientDrawable(
                                GradientDrawable.Orientation.LEFT_RIGHT, new int[] { left,
                                                center, right });
                Resources resources = getResources();
                maskColor = resources.getColor(R.color.viewfinder_mask);
                resultColor = resources.getColor(R.color.result_view);
                resultPointColor = resources.getColor(R.color.possible_result_points);
                laserColor = resources.getColor(R.color.viewfinder_laser);
                scannerAlpha = 0;
                possibleResultPoints = new ArrayList<ResultPoint>(5);
                lastPossibleResultPoints = null;
        }

        private CameraManager cameraManager;

        public void setCameraManager(CameraManager cameraManager) {
                this.cameraManager = cameraManager;
        }

        @Override
        public void onDraw(Canvas canvas) {
                drawBackground(canvas);
        }
        //画背景
        private void drawBackground(Canvas canvas) {
                RectF frame = cameraManager.getFramingRect();
                if (frame == null) {
                        return;
                }
                //屏幕的宽,高
                int width = canvas.getWidth();
                int height = canvas.getHeight();
                // Draw the exterior (i.e. outside the framing rect) darkened
                paint.setColor(resultBitmap != null ? resultColor : maskColor);
                //上部分的mask
                canvas.drawRect(0, 0, width, frame.top, paint);
                //左边的mask
                canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
                //右边的mask
                canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1,
                                paint);
                //底部的mask
                canvas.drawRect(0, frame.bottom + 1, width, height, paint);
                if (resultBitmap != null) {
                        // Draw the opaque result bitmap over the scanning rectangle
                        paint.setAlpha(OPAQUE);
                        canvas.drawBitmap(resultBitmap, frame.left, frame.top, paint);
                } else {
                        paint.setColor(Color.GREEN);
                        // 画出四个角
                        canvas.drawRect(frame.left - 10, frame.top - 10, frame.left + 20,
                                        frame.top, paint);
                        canvas.drawRect(frame.left - 10, frame.top - 10, frame.left,
                                        frame.top + 20, paint);
                        
                        canvas.drawRect(frame.right - 20, frame.top - 10, frame.right + 10,
                                        frame.top, paint);
                        canvas.drawRect(frame.right, frame.top - 10, frame.right + 10,
                                        frame.top + 20, paint);
                        
                        canvas.drawRect(frame.left - 10, frame.bottom,
                                        frame.left + 20, frame.bottom + 10, paint);
                        canvas.drawRect(frame.left - 10, frame.bottom - 20, frame.left,
                                        frame.bottom, paint);
                        
                        canvas.drawRect(frame.right - 20, frame.bottom,
                                        frame.right + 10, frame.bottom + 10, paint);
                        canvas.drawRect(frame.right, frame.bottom - 20, frame.right + 10,
                                        frame.bottom, paint);
                        
                        //划线
                        drawLine(canvas, frame);
                        //刷新camera
//                        postInvalidateDelayed(ANIMATION_DELAY, (int) frame.left, (int) frame.top,
//                                        (int) frame.right, (int) frame.bottom);
                }
        }
        
        private void drawLine(Canvas canvas, RectF frame) {
                paint.setColor(laserColor);
//                paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
//                scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
                if (laserLinePortrait) {
                        if ((i += 4) < frame.bottom - frame.top) {
                                /*
                                 * canvas.drawRect(frame.left + 2, frame.top - 2 + i,
                                 * frame.right - 1, frame.top + 2 + i, paint);
                                 */
//                                int r = 8;
//                                mDrawable.setShape(GradientDrawable.RECTANGLE);
//                                mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
//                                setCornerRadii(mDrawable, r, r, r, r);
//                                mRect.set((int) frame.left + 2, (int) frame.top - 3 + i,
//                                                (int) frame.right - 1, (int) frame.top + 3 + i);
//                                mDrawable.setBounds(mRect);
//                                mDrawable.draw(canvas);
//                                invalidate();
                                
                                canvas.drawRect(frame.left, frame.top, frame.right, frame.top + i, paint);
                                paint.setColor(Color.GREEN);
                                canvas.drawRect(frame.left + 1, frame.top - 1 + i,
                                                 frame.right - 1, frame.top + 2 + i, paint);
                        } else {
                                i = 0;
                        }
                        invalidate((int) frame.left, (int) frame.top,
                                        (int) frame.right, (int) frame.bottom);
                } else {
                        float left = frame.left + (frame.right - frame.left) / 2 - 2;
                        canvas.drawRect(left, frame.top, left + 2, frame.bottom - 2, paint);
                }
        }

        void drawCircle(Canvas canvas, RectF frame) {
//                float middle = frame.height() / 2 + frame.top;
//                canvas.drawRect(frame.left + 2, middle - 1, frame.right - 1,
//                                middle + 2, paint);

                RectF previewFrame = cameraManager.getFramingRectInPreview();
                float scaleX = frame.width() / (float) previewFrame.width();
                float scaleY = frame.height() / (float) previewFrame.height();

                List<ResultPoint> currentPossible = possibleResultPoints;
                List<ResultPoint> currentLast = lastPossibleResultPoints;
                float frameLeft = frame.left;
                float frameTop = frame.top;
                if (currentPossible.isEmpty()) {
                        lastPossibleResultPoints = null;
                } else {
                        // possibleResultPoints = new ArrayList<ResultPoint>(5);
                        lastPossibleResultPoints = currentPossible;
                        paint.setAlpha(CURRENT_POINT_OPACITY);
                        paint.setColor(resultPointColor);
                        synchronized (currentPossible) {
                                for (ResultPoint point : currentPossible) {
                                        canvas.drawCircle(
                                                        frameLeft + (int) (point.getX() * scaleX), frameTop
                                                                        + (int) (point.getY() * scaleY),
                                                        POINT_SIZE, paint);
                                }
                        }
                }
                if (currentLast != null) {
                        paint.setAlpha(CURRENT_POINT_OPACITY / 2);
                        paint.setColor(resultPointColor);
                        synchronized (currentLast) {
                                float radius = POINT_SIZE / 2.0f;
                                for (ResultPoint point : currentLast) {
                                        canvas.drawCircle(
                                                        frameLeft + (int) (point.getX() * scaleX), frameTop
                                                                        + (int) (point.getY() * scaleY), radius,
                                                        paint);
                                }
                        }
                }
        }
        
        public void drawViewfinder() {
                resultBitmap = null;
                invalidate();
        }

        public void setCornerRadii(GradientDrawable drawable, float r0, float r1,
                        float r2, float r3) {
                drawable.setCornerRadii(new float[] { r0, r0, r1, r1, r2, r2, r3, r3 });
        }

        /**
         * Draw a bitmap with the result points highlighted instead of the live
         * scanning display.
         * 
         * @param barcode
         *            An image of the decoded barcode.
         */
        public void drawResultBitmap(Bitmap barcode) {
                resultBitmap = barcode;
                invalidate();
        }

        public void addPossibleResultPoint(ResultPoint point) {
                possibleResultPoints.add(point);
        }

}

调用代码

cameraManager = new CameraManager(this);
                mSurfaceview = (SurfaceView) this.findViewById(R.id.preview_view_photo);
                surfaceHolder = mSurfaceview.getHolder();
                ViewCameraView viewCamerarView = (ViewCameraView) findViewById(R.id.viewcamera_view);
                viewCamerarView.setCameraManager(cameraManager);


详细请见:http://www.szcomtop.cn/forum.php?mod=viewthread&tid=606&highlight=%D4%A4%C0%C0

共有 人打赏支持
粉丝 0
博文 2
码字总数 1720
×
Any_命运
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: