文档章节

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

Any_命运
 Any_命运
发布于 2014/05/10 18:00
字数 819
阅读 80
收藏 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
这可能是关于动态面板,最细致的讲解了

作者:拼搏的80后 全文共 2420 字 6 图,阅读需要 5 分钟 ———— / BEGIN / ———— 动态面板是Axure中使用频率最高的元件之一,动态面板专门用于设计原型中的动态功能,它可以包含一个或...

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

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

码农大表哥
2017/12/06
0
0

没有更多内容

加载失败,请刷新页面

加载更多

原型模式

1、原型模式-定义 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象 克隆(浅度克隆->拷贝值类型或者引用,深度克隆->创建新的对象,开辟新的内存) 例如客户端知道抽象Pro...

阿元
今天
29
0
awk命令扩展使用操作

awk 中使用外部shell变量 示例1 [root@centos01 t1022]# A=888[root@centos01 t1022]# echo "" | awk -v GET_A=$A '{print GET_A}'888[root@centos01 t1022]# echo "aaaaaaaaaaaaa" | aw......

野雪球
今天
27
0
深入解析MySQL视图VIEW

Q:什么是视图?视图是干什么用的? A:视图(view)是一种虚拟存在的表,是一个逻辑表,本身并不包含数据。作为一个select语句保存在数据字典中的。   通过视图,可以展现基表的部分数据;...

IT--小哥
今天
33
0
虚拟机学习之二:垃圾收集器和内存分配策略

1.对象是否可回收 1.1引用计数算法 引用计数算法:给对象中添加一个引用计数器,每当有一个地方引用它时,计数器值就加1;当引用失效时,计数器值就减1;任何时候计数器值为0的对象就是不可能...

贾峰uk
今天
20
0
smart-doc功能使用介绍

smart-doc从8月份底开始开源发布到目前为止已经迭代了几个版本。在这里非常感谢那些敢于用smart-doc去做尝试并积极提出建议的社区用户。因此决定在本博客中重要说明下smart-doc的功能,包括使...

上官胡闹
昨天
32
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部