您的位置:首页 > 移动开发 > Android开发

android扫描二维码:已经封装为jar包,实现扫描只需一步

2016-01-17 03:06 661 查看
介绍一下我自己封装的一个利用google的ZXing扫描二维码的一个jar

文章最后有所有资源的下载链接

1.来源:

在google的ZXingRawProject项目基础上去除和扫描无关的一些类,修改和封装了部分类,使用起来更方便

到底有多方便呢,方便到只需要你重写一个方法,仅仅只需要你写一个,是的你没有看错,真的只需要一个,够方便了吧




2.使用:

(1)导入zxingdecode.jar(自己封装的jar)和core-3.0.0.jar(google二维码扫描和生成所需jar)

文章最后有所有资源的下载链接

(2)添加权限

(3)new QrcodeDecode类并重写handleDecode(Bundle bundle)方法

(4)大功告成

注意事项和说明

实例化QrcodeDecode类需要三个参数 Activity activity,SurfaceView scanPreview ,View scanCropView

activity不说了

scanPreview 相机预览时显示

scanCropView 扫描框的容器

QrcodeDecode是抽象类,必须重写handleDecode(Bundle bundle)方法,当扫描二维码成功后会调用该方法:

参数bundle封装了扫描结果:

bundle.getInt(QrcodeDecode.BARCODE_WIDTH)图片宽度;

bundle.getInt(QrcodeDecode.BARCODE_HEIGHT)图片高度 ;

bundle.getString(QrcodeDecode.BARCODE_RESULT) 扫描结果;

bundle.getByteArray(QrcodeDecode.BARCODE_BITMAP 截取的图像.

注意:

为节省资源可以根据Activity 的生命周期来调用QrcodeDecode的方法用于释放资源

介绍完毕,下面开始上demo代码

1:权限:

<uses-permission android:name="android.permission.CAMERA" />

    <uses-permission android:name="android.permission.FLASHLIGHT" />

    <uses-feature android:name="android.hardware.camera" />

    <uses-feature android:name="android.hardware.camera.autofocus" />

    <uses-permission android:name="android.permission.VIBRATE" />

    <uses-permission android:name="android.permission.WAKE_LOCK" />

2:扫描的界面:activity_capture.xml

<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical" >

<SurfaceView
android:id="@+id/capture_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<RelativeLayout
android:id="@+id/capture_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/capture_mask_top"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_alignParentTop="true"
android:background="@drawable/shadow" />

<RelativeLayout
android:id="@+id/capture_crop_view"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@id/capture_mask_top"
android:layout_centerHorizontal="true"
android:background="@drawable/qr_code_bg" >

<ImageView
android:id="@+id/capture_scan_line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/scan_line" />
</RelativeLayout>

<ImageView
android:id="@+id/capture_mask_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@id/capture_crop_view"
android:background="@drawable/shadow" />

<ImageView
android:id="@+id/capture_mask_left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="@id/capture_mask_bottom"
android:layout_alignParentLeft="true"
android:layout_below="@id/capture_mask_top"
android:layout_toLeftOf="@id/capture_crop_view"
android:background="@drawable/shadow" />

<ImageView
android:id="@+id/capture_mask_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="@id/capture_mask_bottom"
android:layout_alignParentRight="true"
android:layout_below="@id/capture_mask_top"
android:layout_toRightOf="@id/capture_crop_view"
android:background="@drawable/shadow" />
</RelativeLayout>

</RelativeLayout>



3:扫描成功之后,用于展示扫描结果的界面activity_result.xml

<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:gravity="center_horizontal"
android:orientation="vertical" >

<ImageView
android:id="@+id/result_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:scaleType="fitXY" />

<TextView
android:id="@+id/result_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:background="#D8D8D8"
android:gravity="left|top"
android:padding="10dp"
android:textColor="@android:color/black"
android:textSize="14sp" />

</LinearLayout>



4:首页,扫描二维码页面:MainActivity.java

</pre><pre name="code" class="java">package com.example.decode;

import com.rbj.zxing.decode.QrcodeDecode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

private SurfaceView scanPreview;//相机
private RelativeLayout scanCropView;//扫描框
private ImageView scanLine;//扫描框中间的线

private QrcodeDecode qd;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_capture);
scanPreview = (SurfaceView) findViewById(R.id.capture_preview);
scanCropView = (RelativeLayout) findViewById(R.id.capture_crop_view);
scanLine = (ImageView) findViewById(R.id.capture_scan_line);

qd = new QrcodeDecode(this,scanPreview,scanCropView) {

@Override
public void handleDecode(Bundle bundle) {
//扫描成功后调用
startActivity(new Intent(MainActivity.this, ResultActivity.class).putExtras(bundle));

}
};
TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT,
0.9f);
animation.setDuration(4500);
animation.setRepeatCount(-1);
animation.setRepeatMode(Animation.RESTART);
scanLine.startAnimation(animation);
}

@Override
protected void onResume() {
super.onResume();
//在此处开起扫描
qd.onResume();
}

@Override
protected void onPause() {
//
qd.onPause();
super.onPause();
}

@Override
protected void onDestroy() {
//释放资源
qd.onDestroy();
super.onDestroy();
}
}


5:结果展示界面:ResultActivity.java

package com.example.decode;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.TypedValue;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.rbj.zxing.decode.QrcodeDecode;

public class ResultActivity extends Activity {

private ImageView mResultImage;
private TextView mResultText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);

Bundle extras = getIntent().getExtras();

mResultImage = (ImageView) findViewById(R.id.result_image);
mResultText = (TextView) findViewById(R.id.result_text);

if (null != extras) {
int width = extras.getInt("width");
int height = extras.getInt("height");

LayoutParams lps = new LayoutParams(width, height);
lps.topMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, getResources().getDisplayMetrics());
lps.leftMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());
lps.rightMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());

mResultImage.setLayoutParams(lps);

String result = extras.getString(QrcodeDecode.BARCODE_RESULT);
mResultText.setText(result);

Bitmap barcode = null;
byte[] compressedBitmap = extras.getByteArray(QrcodeDecode.BARCODE_BITMAP);
if (compressedBitmap != null) {
barcode = BitmapFactory.decodeByteArray(compressedBitmap, 0, compressedBitmap.length, null);
// Mutable copy:
barcode = barcode.copy(Bitmap.Config.RGB_565, true);
}

mResultImage.setImageBitmap(barcode);
}
}
}

资源下载

zxingdecode.jar下载地址
http://download.csdn.net/detail/rongbaojian/9407087
core-3.0.0.jar下载地址
http://download.csdn.net/download/rongbaojian/9407088

Decode.zip

http://download.csdn.net/detail/rongbaojian/9407090
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: