我们在做项目中很多时候会用到二维码功能,当然我们不用去自己写二维功能,如果自己写那编码,解码太麻烦了,google有一个开源项目叫做zxing是一个二维码的功能,非常的详细,包装的也非常的好,我们只需要改一改就能拿来自己用,想定义成什么样式的二维样式都可以。在这里我抽取了eoe android客户端中的二维码扫描功能,简单的说一下,还是老步骤先上图,无图无真相。
以下是要打开二维码的界面和二维扫描的界面
一、我们看一下代码实现方式:注意我们在开发二维功能的时候,先要导入一个jar包,在zxing的core中打包的,这里用的是zxing-2.2-core.jar。
1、先看一下包结构:
2、以上代码除了com.jun.zxingtext中的代码,剩下的代码都是从zxing项目中拿出来的,当然zxing项目中有很多的代码,用这几个包中的部分代码就够了。
在这里主要说一下自定义二维码扫描的在com.google.zxing.view-->ViewfinderView.java
/*
* Copyright (C) 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.view;
import java.util.Collection;
import java.util.HashSet;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import com.google.zxing.ResultPoint;
import com.google.zxing.camera.CameraManager;
import com.jun.zxingtest.R;
/**
* 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.
* 自定义二维码扫描区域样式
*/
public final class ViewfinderView extends View {
private static final long ANIMATION_DELAY = 100L;
private static final int OPAQUE = 0xFF;
private static final int SPEEN_DISTANCE = 8; //扫描线的下落速度
private final Paint paint;
private Bitmap resultBitmap;
private final int maskColor;
private final int resultColor;
private final int frameColor;
private final int resultPointColor;
private Collection possibleResultPoints;
private Collection lastPossibleResultPoints;
private boolean isFirst;
private int slideTop;
private int slideBottom;
private Bitmap qrLineBitmap;//微信的扫描线是一张图片
private int qrWidth;//扫描线的长
private int qrHeight;//扫描线的高
private Rect qrSrc;
private Rect qrDst;
// This constructor is used when the class is built from an XML resource.
//此构造方法是用来从xml文件中加载的时候调用的
public ViewfinderView(Context context, AttributeSet attrs) {
super(context, attrs);
// Initialize these once for performance rather than calling them every
// time in onDraw().
paint = new Paint();
Resources resources = getResources();
qrLineBitmap = BitmapFactory.decodeResource(resources, R.drawable.qrcode_scan_line);
qrWidth = qrLineBitmap.getWidth();
qrHeight = qrLineBitmap.getHeight();
qrSrc=new Rect(0, 0, qrWidth, qrHeight);
maskColor = resources.getColor(R.color.viewfinder_mask);
resultColor = resources.getColor(R.color.result_view);
frameColor = resources.getColor(R.color.viewfinder_frame);
resultPointColor = resources.getColor(R.color.possible_result_points);
possibleResultPoints = new HashSet(5);
}
@Override
public void onDraw(Canvas canvas) {
Rect frame = CameraManager.get().getFramingRect();
if (frame == null) {
return;
}
//初始化中间线滑动的最上边和最下边
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 {
//绘制中间的线,每次刷新界面,中间的线往下移动SPEEN_DISTANCE
slideTop += SPEEN_DISTANCE;
if(slideTop >= frame.bottom){
slideTop = frame.top;
}
qrDst=new Rect(frame.left, slideTop, frame.right, slideTop+qrHeight);
canvas.drawBitmap(qrLineBitmap,qrSrc,qrDst ,null);
int linewidth = 10;
paint.setColor(frameColor);
// draw rect 画扫描区域的四个直角
canvas.drawRect(15 + frame.left, 15 + frame.top,
15 + (linewidth + frame.left), 15 + (50 + frame.top), paint);
canvas.drawRect(15 + frame.left, 15 + frame.top,
15 + (50 + frame.left), 15 + (linewidth + frame.top), paint);
canvas.drawRect(-15 + ((0 - linewidth) + frame.right),
15 + frame.top, -15 + (1 + frame.right),
15 + (50 + frame.top), paint);
canvas.drawRect(-15 + (-50 + frame.right), 15 + frame.top, -15
+ frame.right, 15 + (linewidth + frame.top), paint);
canvas.drawRect(15 + frame.left, -15 + (-49 + frame.bottom),
15 + (linewidth + frame.left), -15 + (1 + frame.bottom),
paint);
canvas.drawRect(15 + frame.left, -15
+ ((0 - linewidth) + frame.bottom), 15 + (50 + frame.left),
-15 + (1 + frame.bottom), paint);
canvas.drawRect(-15 + ((0 - linewidth) + frame.right), -15
+ (-49 + frame.bottom), -15 + (1 + frame.right), -15
+ (1 + frame.bottom), paint);
canvas.drawRect(-15 + (-50 + frame.right), -15
+ ((0 - linewidth) + frame.bottom), -15 + frame.right, -15
+ (linewidth - (linewidth - 1) + frame.bottom), paint);
Collection currentPossible = possibleResultPoints;
Collection currentLast = lastPossibleResultPoints;
if (currentPossible.isEmpty()) {
lastPossibleResultPoints = null;
} else {
possibleResultPoints = new HashSet(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);
}
}
// Request another update at the animation interval, but only
// repaint the laser line,
// not the entire viewfinder mask.
postInvalidateDelayed(ANIMATION_DELAY, frame.left, frame.top,
frame.right, frame.bottom);
}
}
public void drawViewfinder() {
resultBitmap = null;
invalidate();
}
/**
* 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);
}
}
其它的不用再多说,看代码就可以,下面我们看一下如何调用二维码扫描到的结果在com.jun.zxingtest-->MainActivity.java中 它是通过startactivityforresult来获取结果的。
package com.jun.zxingtest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.CaptureActivity;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
static final private int GET_CODE = 0;
private TextView show_scan_result ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.findViewById(R.id.scan).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(MainActivity.this,
CaptureActivity.class), GET_CODE);
}
});
show_scan_result = (TextView) this.findViewById(R.id.show_result) ;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode == RESULT_OK && requestCode == GET_CODE) {
String result = data.getExtras().getString("result");
if(!result.isEmpty()){
Toast.makeText(MainActivity.this, "结果是:" + result, 0).show();
show_scan_result.setText(String.format("%s", "扫描的结果是:"+result)) ;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
最后附上源码下载地址:http://download.csdn.net/detail/android0012345/6461185