canvas教程

android二维码扫描(zxing使用小结)

字号+ 作者:H5之家 来源:H5之家 2015-10-27 08:33 我要评论( )

android二维码扫描(zxing使用小结)

主要有三点:

1、界面绘制,包括4个边角、上下滚动的横线、在画布上添加文字

2、调整二维码扫描框的位置。

3、解决图片被压变形的问题。


1、界面绘制

ViewfinderView.java中onDraw()方法

@Override public void onDraw(Canvas canvas) { // 中间的扫描框,你要修改扫描框的大小,去CameraManager里面修改 Rect frame = CameraManager.get().getFramingRect(); int top = frame.top; int left = frame.left; int right = frame.right; int bottom = frame.bottom; frame = new Rect(left , top, right , bottom ); // 初始化中间线滑动的最上边和最下边 if (!isFirst) { isFirst = true; slideTop = frame.top; slideBottom = frame.bottom; } // 获取屏幕的宽和高 int width = canvas.getWidth(); int height = canvas.getHeight(); // 画出扫描框外面的阴影部分,共四个部分,扫描框的上面到屏幕上面,扫描框的下面到屏幕下面 // 扫描框的左边面到屏幕左边,扫描框的右边到屏幕右边 // Draw the exterior (i.e. outside the framing rect) darkened paint.setColor(resultBitmap != null ? resultColor : maskColor); canvas.drawRect(0, 0, width, frame.top, paint); canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint); canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint); 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 { // 画扫描框边上的角,总共8个部分 paint.setColor(Color.GREEN); canvas.drawRect(frame.left, frame.top, frame.left + ScreenRate, frame.top + CORNER_WIDTH, paint); canvas.drawRect(frame.left, frame.top, frame.left + CORNER_WIDTH, frame.top + ScreenRate, paint); canvas.drawRect(frame.right - ScreenRate, frame.top, frame.right, frame.top + CORNER_WIDTH, paint); canvas.drawRect(frame.right - CORNER_WIDTH, frame.top, frame.right, frame.top + ScreenRate, paint); canvas.drawRect(frame.left, frame.bottom - CORNER_WIDTH, frame.left + ScreenRate, frame.bottom, paint); canvas.drawRect(frame.left, frame.bottom - ScreenRate, frame.left + CORNER_WIDTH, frame.bottom, paint); canvas.drawRect(frame.right - ScreenRate, frame.bottom - CORNER_WIDTH, frame.right, frame.bottom, paint); canvas.drawRect(frame.right - CORNER_WIDTH, frame.bottom - ScreenRate, frame.right, frame.bottom, paint); //绘制中间的线,每次刷新界面,中间的线往下移动SPEEN_DISTANCE slideTop += SPEEN_DISTANCE; if(slideTop >= frame.bottom){ slideTop = frame.top; } Rect lineRect = new Rect(); lineRect.left = frame.left; lineRect.right = frame.right; lineRect.top = slideTop; lineRect.bottom = slideTop + 18; canvas.drawBitmap(((BitmapDrawable)(getResources().getDrawable(R.drawable.qrcode_scan_line))).getBitmap(), null, lineRect, paint); //画扫描框下面的字 // paint.setColor(Color.WHITE); // paint.setTextSize(TEXT_SIZE * density); // paint.setAlpha(0x40); // paint.setTypeface(Typeface.create("System", Typeface.BOLD)); // String text = "将二维码放入框内, 即可自动扫描"; // float textWidth = paint.measureText(text); // // canvas.drawText(text, (width - textWidth)/2, (float) (frame.bottom + (float)TEXT_PADDING_TOP *density), paint); Collection<ResultPoint> currentPossible = possibleResultPoints; Collection<ResultPoint> currentLast = lastPossibleResultPoints; if (currentPossible.isEmpty()) { lastPossibleResultPoints = null; } else { possibleResultPoints = new HashSet<ResultPoint>(5); lastPossibleResultPoints = currentPossible; paint.setAlpha(OPAQUE); paint.setColor(resultPointColor); for (ResultPoint point : currentPossible) { canvas.drawCircle(frame.left + point.getX(), frame.top + point.getY(), 6.0f, paint); } } if (currentLast != null) { paint.setAlpha(OPAQUE / 2); paint.setColor(resultPointColor); for (ResultPoint point : currentLast) { canvas.drawCircle(frame.left + point.getX(), frame.top + point.getY(), 3.0f, paint); } } //只刷新扫描框的内容,其他地方不刷新 postInvalidateDelayed(ANIMATION_DELAY, frame.left, frame.top, frame.right, frame.bottom); } }

2、调整位置

如果不设置全屏,扫描框的位置和取图的位置高度目测会有50px误差,

不全屏公式:((Y - 标题栏 - 状态栏)/2 - 扫描框高度)/2 = y轴坐标

只要不全屏:就会有误差,导致框中的二维码靠上或者远离二维码才会扫描成功

如果设置为全屏,就不会有误差,请了解内情的大神告知笔者,想知道什么原因造成

CameraManager.java 中 getFramingRect()方法  

public Rect getFramingRect() { Point screenResolution = configManager.getScreenResolution(); if (framingRect == null) { if (camera == null) { return null; } /* 确定在固定比例分辨率下二维码扫描框的宽高*/ int width = screenResolution.x * 3 / 4; if (width < MIN_FRAME_WIDTH) { width = MIN_FRAME_WIDTH; } else if (width > MAX_FRAME_WIDTH) { width = MAX_FRAME_WIDTH; } int height = screenResolution.y * 3 / 4; if (height < MIN_FRAME_HEIGHT) { height = MIN_FRAME_HEIGHT; } else if (height > MAX_FRAME_HEIGHT) { height = MAX_FRAME_HEIGHT; } //计算x轴y轴坐标,可在此调整二维码框的位置 int leftOffset = (screenResolution.x - width) / 2 ; int topOffset = ((screenResolution.y) /2 - height) / 2 ; framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height); Log.d(TAG, "Calculated framing rect: " + framingRect); } return framingRect; }
3、框中图片变形

CameraConfigurationManager.java中void setDesiredCameraParameters(Camera camera)方法


 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Canvas与ValueAnimator

    Canvas与ValueAnimator

    2017-04-28 18:00

  • 21天学习android开发教程之SurfaceView与多线程的混搭

    21天学习android开发教程之SurfaceView与多线程的混搭

    2017-04-27 12:00

  • PolygonDrawingUtil

    PolygonDrawingUtil

    2017-04-26 18:02

  • Android开发基本常识及技巧

    Android开发基本常识及技巧

    2017-04-13 16:02

网友点评