最近开发的项目中又涉及到二维码识别的技术,主要是做二维码扫码支付功能,我就从网上找了一个高逼格的可以自定义的三方库进行使用(使用的是博客一片枫叶_刘超,博客http://my.csdn.net/qq_23547831),首先看一下界面效果
首先你要引入三方库文件
compile 'cn.yipianfengye.android:zxing-library:2.2'
Java代码如下
case R.id.button3://定制化二维码扫描模式 intent = new Intent(MainActivity.this, SecondActivity.class); startActivityForResult(intent, REQUEST_CODE); break;
这里的SecondActivity就是自定义的Activity
只是比之前的CaptureActivity多了一行代码
CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera);这里的布局文件中引用了一个自定义的控件ViewFinerView,他是继承自View的
扫描二维码关注公众号,回复:
147957 查看本文章
<declare-styleable name="ViewfinderView"> <attr name="inner_width" format="dimension"/> <attr name="inner_height" format="dimension"/> <attr name="inner_margintop" format="dimension" /> <attr name="inner_corner_color" format="color" /> <attr name="inner_corner_length" format="dimension" /> <attr name="inner_corner_width" format="dimension" /> <attr name="inner_scan_bitmap" format="reference" /> <attr name="inner_scan_speed" format="integer" /> <attr name="inner_scan_iscircle" format="boolean" /> </declare-styleable>
从上面的图片中你看到的所有矩形(包括上下左右的黑框和扫描的四个角的八个矩形)都是通过Canvas.drawRect(left,top,right,bottom)方法绘制的
/** * 自定义组件实现,扫描功能 */ public final class ViewfinderView extends View { private static final long ANIMATION_DELAY = 100L; private static final int OPAQUE = 0xFF; private final Paint paint; private Bitmap resultBitmap; private final int maskColor; private final int resultColor; private final int resultPointColor; private Collection<ResultPoint> possibleResultPoints; private Collection<ResultPoint> lastPossibleResultPoints; // 扫描线移动的y private int scanLineTop; // 扫描线移动速度 private int SCAN_VELOCITY; // 扫描线 private Bitmap scanLight; // 是否展示小圆点 private boolean isCircle; public ViewfinderView(Context context) { this(context, null); } public ViewfinderView(Context context, AttributeSet attrs) { this(context, attrs, -1); } public ViewfinderView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); paint = new Paint(); 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); possibleResultPoints = new HashSet<>(5); scanLight = BitmapFactory.decodeResource(resources, R.drawable.scan_light); initInnerRect(context, attrs); } /** * 初始化内部框的大小 * * @param context * @param attrs */ private void initInnerRect(Context context, AttributeSet attrs) { TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ViewfinderView); // 扫描框距离顶部 float innerMarginTop = ta.getDimension(R.styleable.ViewfinderView_inner_margintop, -1); if (innerMarginTop != -1) { CameraManager.FRAME_MARGINTOP = (int) innerMarginTop; } // 扫描框的宽度 CameraManager.FRAME_WIDTH = (int) ta.getDimension(R.styleable.ViewfinderView_inner_width, DisplayUtil.screenWidthPx / 2); // 扫描框的高度 CameraManager.FRAME_HEIGHT = (int) ta.getDimension(R.styleable.ViewfinderView_inner_height, DisplayUtil.screenWidthPx / 2); // 扫描框边角颜色 innercornercolor = ta.getColor(R.styleable.ViewfinderView_inner_corner_color, Color.parseColor("#45DDDD")); // 扫描框边角长度 innercornerlength = (int) ta.getDimension(R.styleable.ViewfinderView_inner_corner_length, 65); // 扫描框边角宽度 innercornerwidth = (int) ta.getDimension(R.styleable.ViewfinderView_inner_corner_width, 15); // 扫描bitmap Drawable drawable = ta.getDrawable(R.styleable.ViewfinderView_inner_scan_bitmap); if (drawable != null) { } // 扫描控件 scanLight = BitmapFactory.decodeResource(getResources(), ta.getResourceId(R.styleable.ViewfinderView_inner_scan_bitmap, R.drawable.scan_light)); // 扫描速度 SCAN_VELOCITY = ta.getInt(R.styleable.ViewfinderView_inner_scan_speed, 5); isCircle = ta.getBoolean(R.styleable.ViewfinderView_inner_scan_iscircle, true); ta.recycle(); } @Override public void onDraw(Canvas canvas) { Rect frame = CameraManager.get().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); 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 { drawFrameBounds(canvas, frame); drawScanLight(canvas, frame); 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); if (isCircle) { 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); if (isCircle) { 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); } } // 扫描框边角颜色 private int innercornercolor; // 扫描框边角长度 private int innercornerlength; // 扫描框边角宽度 private int innercornerwidth; /** * 绘制取景框边框 * * @param canvas * @param frame */ private void drawFrameBounds(Canvas canvas, Rect frame) { /*paint.setColor(Color.WHITE); paint.setStrokeWidth(2); paint.setStyle(Paint.Style.STROKE); canvas.drawRect(frame, paint);*/ paint.setColor(innercornercolor); paint.setStyle(Paint.Style.FILL); int corWidth = innercornerwidth; int corLength = innercornerlength; // 左上角 canvas.drawRect(frame.left, frame.top, frame.left + corWidth, frame.top + corLength, paint); canvas.drawRect(frame.left, frame.top, frame.left + corLength, frame.top + corWidth, paint); // 右上角 canvas.drawRect(frame.right - corWidth, frame.top, frame.right, frame.top + corLength, paint); canvas.drawRect(frame.right - corLength, frame.top, frame.right, frame.top + corWidth, paint); // 左下角 canvas.drawRect(frame.left, frame.bottom - corLength, frame.left + corWidth, frame.bottom, paint); canvas.drawRect(frame.left, frame.bottom - corWidth, frame.left + corLength, frame.bottom, paint); // 右下角 canvas.drawRect(frame.right - corWidth, frame.bottom - corLength, frame.right, frame.bottom, paint); canvas.drawRect(frame.right - corLength, frame.bottom - corWidth, frame.right, frame.bottom, paint); } /** * 绘制移动扫描线 * * @param canvas * @param frame */ private void drawScanLight(Canvas canvas, Rect frame) { if (scanLineTop == 0) { scanLineTop = frame.top; } if (scanLineTop >= frame.bottom - 30) { scanLineTop = frame.top; } else { scanLineTop += SCAN_VELOCITY; } Rect scanRect = new Rect(frame.left, scanLineTop, frame.right, scanLineTop + 30); canvas.drawBitmap(scanLight, null, scanRect, paint); } public void drawViewfinder() { resultBitmap = null; invalidate(); } public void addPossibleResultPoint(ResultPoint point) { possibleResultPoints.add(point); } /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } }用户可以根据自己的需求进行适当的修改颜色和文字等内容
最后附上github地址 https://github.com/yipianfengye/android-zxingLibrary