文档章节

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

Any_命运
 Any_命运
发布于 2014/05/10 18:00
字数 819
阅读 72
收藏 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 相机使用教程(二)

上一篇介绍了如何使用系统相机简单、快速的进行拍照,本篇将介绍如何使用框架提供的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
如何免费下载高程数据并生成TIF文件

工具:WOLFMAP地图下载器(这里就不提供下载地址了,百度上搜索各大下载站均可下载) 这里我以下载富士山为例。打开软件,地点跳转到富士山,这里还可以看见影像拍摄日期哦。然后在地图菜单里...

umi-1
2016/07/13
107
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

C++ gflags

###定义参数 gflags主要支持的参数类型包括bool,int32, int64, uint64, double, string等,定义参数通过DEFINE_type宏实现, 该宏的三个参数含义分别为命令行参数名,参数默认值,以及参数的...

SibylY
17分钟前
0
0
intellij IDEA Properties中文unicode转码问题

在IDEA中创建了properties文件,发现默认中文不会自动进行unicode转码。如下 在project settings - File Encoding,在标红的选项上打上勾,确定即可 效果图如下: unicode转码后效果...

muzi1994
17分钟前
0
0
Java IO类库之PipedWriter

一、PipedWriter介绍 PipedWriter是字符管道输出流,继承自Writer,功能与PipedOutputStream类似,通过与PipedReader组合使用实现类似管道的功能,在多线程环境下,一个线程使用PipedWriter...

老韭菜
21分钟前
0
0
精简分页组件(手写)

需要引入CSS(没错就是这4行) .pagelist { text-align: center; color: #666; width: 100%; clear: both; margin: 20px 0; padding-top: 20px }.pagelist a { color: #666; margin: 0 2px;......

AK灬
22分钟前
3
0
29 岁成为阿里巴巴 P8,工作前 5 年完成晋升 3 连跳,他如何做到?

泡泡是我的好朋友。今年 31 岁,毕业后就进了阿里巴巴,工作五年内从 P4 晋升至 P6、P7、P8。 和他很少聊到工作,但总觉得他有很棒的职场心得,应该分享出来,于是有了这次采访。希望对职场新...

Java填坑之路
24分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部