文档章节

只拍摄SurfaceView预览界面特定区域内容(矩形框)

Any_命运
 Any_命运
发布于 2014/05/10 18:00
字数 819
阅读 761
收藏 0

阿里云携手百名商业领袖、技术大咖,带您一探行进中的数字新基建!>>>

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

© 著作权归作者所有

Any_命运
粉丝 0
博文 2
码字总数 1720
作品 0
深圳
程序员
私信 提问
加载中

评论(0)

Android ORC文字识别之识别身份证号等(附源码)

项目地址 https://github.com/979451341/OrcTest 我们说说实现这个项目已实现的功能,能够截图手机界面的某一块,将这个某一块图片的Bitmap传给tess-two的代码来获取扫描结果 我这里在贴出t...

osc_cym01vn6
2018/03/06
6
0
Android ORC文字识别之识别身份 证号等(附源码)

项目地址 https://github.com/979451341/OrcTest 我们说说实现这个项目已实现的功能,能够截图手机界面的某一块,将这个某一块图片的Bitmap传给tess-two的代码来获取扫描结果 我这里在贴出t...

qq5a70561b29238
2018/03/06
0
0
Android相机开发那些坑

版权声明:本文由王梓原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/168 来源:腾云阁 https://www.qcloud.com/community 最近我负责开发了一个跟And...

偶素浅小浅
2016/11/08
78
0
组长问我几天能开发一个人见人爱的百万量级Android相机,我是这样回答的...

欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由QQ空间开发团队发表于云+社区专栏 最近我负责开发了一个跟Android相机有关的需求,新功能允许用户使用手机摄像头,快速拍摄...

腾讯云+社区
2018/09/19
0
0
Android Camera2 拍照(二)——使用TextureView

原文:Android Camera2 拍照(二)——使用TextureView 上一篇博文简单介绍了使用Camera2 API拍摄照片,并使用SurfaceView作为预览界面。实际上,相对于SurfaceView, TextureView更适合用于视...

osc_c4xow9p9
2018/06/13
2
0

没有更多内容

加载失败,请刷新页面

加载更多

.NET中小数,浮点数和双精度之间的区别? - Difference between decimal, float and double in .NET?

问题: What is the difference between decimal , float and double in .NET? .NET中的decimal , float和double float什么区别? When would someone use one of these? 有人什么时候会使用......

fyin1314
39分钟前
15
0
如何找出Windows上正在侦听端口的进程? - How can you find out which process is listening on a port on Windows?

问题: 如何找出Windows上正在侦听端口的进程? 解决方案: 参考一: https://stackoom.com/question/CXO/如何找出Windows上正在侦听端口的进程 参考二: https://oldbug.net/q/CXO/How-can...

技术盛宴
今天
10
0
OSChina 周三乱弹 —— 一家动物都快饿成标本了~

@黑觉非常君 :前天晚上9点开始睡觉,睡到昨天上午8点起床,昨天下午2点又睡,睡到下午7点多,晚上10点又困了,又睡,睡到今天上午8点,中途没醒过,怎么这么能睡,是不是快挂了。 能睡不是好...

小小编辑
今天
15
0
神剧推荐全剧最污片段精剪

神剧推荐,全剧最污片段精剪 豆瓣评分最高,脑洞最大,脑回路最曲折,恶搞无数经典,没有一条差评的神剧 整个系列完整版 到这里观看

a57571735
今天
22
0
pingan.

职位诱惑:金融,技术研发,技术攻关,大舞台大作为职位描述:职位诱惑:立志打造一支国内一流实盘量化交易框架开发团队职位描述:职位描述:负责实盘低时延量化交易框架及附属行...

MtrS
今天
15
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部