文档章节

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

Any_命运
 Any_命运
发布于 2014/05/10 18:00
字数 819
阅读 87
收藏 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
深圳
程序员
私信 提问
Android相机开发那些坑

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

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

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

腾讯云+社区
09/19
0
0
Android 相机使用教程(二)

上一篇介绍了如何使用系统相机简单、快速的进行拍照,本篇将介绍如何使用框架提供的API直接控制摄像机硬件。 控制相机 直接控制设备相机比从现有相机应用程序中请求图片或视频要复杂很多。但...

Xiao_Mai
2017/10/13
0
0
大表哥带你一步一步用Builder模式实现自定义相机(拍照+录制,附源码)

1.前言 1.1 初衷 在我们做很多项目的过程中,经常会遇到很多需要自定义的相机的需求,这个时候,很多人第一步都是网上查资料,包括我也是这样,但是我没有发现有比较靠谱没有Bug的开源项目,...

码农大表哥
2017/12/06
0
0
Android Camera 相机开发详解

在Android 5.0(SDK 21)中,Google使用Camera2替代了Camera接口。Camera2在接口和架构上做了巨大的变动,但是基于众所周知的原因,我们还必须基于 Android 4.+ 系统进行开发。本文介绍的是C...

陈小锅
2016/07/25
1K
0

没有更多内容

加载失败,请刷新页面

加载更多

python自然语言处理快速入门2常见的NLP操作

在本章中,我们将讨论我们文本数据进行的一些常见数据预处理操作,以适配典型的机器学习算法,如贝叶斯、决策树,逻辑回归等等。这些算法都只适用于数字向量,而不是文本。 那么我们如何将文...

python测试开发人工智能安全
18分钟前
0
0
OSChina 周一乱弹 —— 温柔的人应该这样

Osc乱弹歌单(2018)请戳(这里) 【今日歌曲】 @clouddyy :#每日一歌# 《フィクション-sumika》 《フィクション-sumika》 手机党少年们想听歌,请使劲儿戳(这里) 假期时间干嘛去, @for...

小小编辑
36分钟前
9
4
[LintCode] Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)

描述 设计一个算法,并编写代码来序列化和反序列化二叉树。将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”。 如何反序列化或序列化二叉树是没有限制的,你...

honeymose
今天
6
0
java框架学习日志-7(静态代理和JDK代理)

静态代理 我们平时去餐厅吃饭,不是直接告诉厨师做什么菜的,而是先告诉服务员点什么菜,然后由服务员传到给厨师,相当于服务员是厨师的代理,我们通过代理让厨师炒菜,这就是代理模式。代理...

白话
今天
26
0
Flink Window

1.Flink窗口 Window Assigner分配器。 窗口可以是时间驱动的(Time Window,例如:每30秒钟),也可以是数据驱动的(Count Window,例如:每一百个元素)。 一种经典的窗口分类可以分成: 翻...

满小茂
今天
19
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部