今天为大家介绍的是我的github开源项目ZxingSimplify,一个精简的安卓Zxing扫码库。有了它你将分分钟集成扫码(包括二维码、条形码等)、相册二维码识别、开关闪光灯等功能。
一.扫码界面
二.使用
1.Gradle添加依赖,此库已同时提交到Jcenter、MavenCentral,最新版本1.0.5。
compile 'com.github.shenyuanqing.zxing:zxing-simplify:1.0.5'
2.跳转到扫码页CaptureActivity。
3.扫码成功在onActivityResult回调方法获得扫码结果。
4.Android6.0以上系统跳转扫码页前先获取运行时权限。代码如下:
package com.github.shenyuanqing.zxingtest;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Toast;
import com.github.shenyuanqing.zxingsimplify.zxing.Activity.CaptureActivity;
public class MainActivity extends Activity {
private Context mContext;
private Activity mActivity;
private static final int REQUEST_SCAN = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
mActivity = this;
init();
}
private void init() {
findViewById(R.id.bt_scan).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getRuntimeRight();
}
});
}
/**
* 获得运行时权限
*/
private void getRuntimeRight() {
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.CAMERA}, 1);
} else {
jumpScanPage();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
jumpScanPage();
} else {
Toast.makeText(mContext, "拒绝", Toast.LENGTH_LONG).show();
}
default:
break;
}
}
/**
* 跳转到扫码页
*/
private void jumpScanPage() {
startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class), REQUEST_SCAN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SCAN && resultCode == RESULT_OK) {
Toast.makeText(MainActivity.this, data.getStringExtra("barCode"), Toast.LENGTH_LONG).show();
}
}
}
三.有些场景可能扫码成功后要先暂停扫码做些处理后再恢复扫码,此库也做了相应的支持(扫码线用属性动画控制,可以记录停止点),先调用CaptureActivity里的pauseScan()方法再调用startScan()方法。
CaptureActivity源码:
package com.github.shenyuanqing.zxingsimplify.zxing.Activity;
import android.Manifest;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.github.shenyuanqing.zxingsimplify.R;
import com.github.shenyuanqing.zxingsimplify.zxing.camera.CameraManager;
import com.github.shenyuanqing.zxingsimplify.zxing.decode.DecodeThread;
import com.github.shenyuanqing.zxingsimplify.zxing.utils.BeepManager;
import com.github.shenyuanqing.zxingsimplify.zxing.utils.CaptureActivityHandler;
import com.github.shenyuanqing.zxingsimplify.zxing.utils.InactivityTimer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.DecodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.Hashtable;
public class CaptureActivity extends Activity implements SurfaceHolder.Callback {
private Context context;
private Activity activity;
private static final String TAG = CaptureActivity.class.getSimpleName();
private TextView tvLight;
private ToggleButton tbLight;
private ImageView ivAlbum;
private ImageView scanLine;
private SurfaceView scanPreview = null;
private RelativeLayout scanContainer;
private RelativeLayout scanCropView;
private static final int REQUEST_ALBUM = 0;
private boolean isPause = false;
private Camera camera;
private CaptureActivityHandler handler;
private Rect mCropRect = null;
private CameraManager cameraManager;
private InactivityTimer inactivityTimer;
private BeepManager beepManager;
private ObjectAnimator objectAnimator;
// private TranslateAnimation translateAnimation;
public Handler getHandler() {
return handler;
}
public CameraManager getCameraManager() {
return cameraManager;
}
private boolean isHasSurface = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_capture);
context = this;
activity = this;
findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
init();
initScan();
}
private void init() {
tvLight = (TextView) findViewById(R.id.tv_light);
ivAlbum = (ImageView) findViewById(R.id.iv_album);
tbLight = (ToggleButton) findViewById(R.id.tb_light);
//闪光灯控制
tbLight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
tvLight.setText("关灯");
openFlashlight();
} else {
tvLight.setText("开灯");
closeFlashlight();
}
}
});
//打开相册
findViewById(R.id.ll_album).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getRuntimeRight();
}
});
}
/**
* 扫码初始化
*/
private void initScan() {
scanPreview = (SurfaceView) findViewById(R.id.capture_preview);
scanContainer = (RelativeLayout) findViewById(R.id.capture_container);
scanCropView = (RelativeLayout) findViewById(R.id.capture_crop_view);
scanLine = (ImageView) findViewById(R.id.scan_line);
//扫描线动画1(属性动画可暂停)
float curTranslationY = scanLine.getTranslationY();
objectAnimator = ObjectAnimator.ofFloat(scanLine, "translationY", curTranslationY, dp2px(this, 170));
objectAnimator.setDuration(4000);
objectAnimator.setInterpolator(new LinearInterpolator());
objectAnimator.setRepeatCount(ObjectAnimator.INFINITE);
objectAnimator.setRepeatMode(ValueAnimator.RESTART);
}
@Override
protected void onResume() {
super.onResume();
startScan();
}
@Override
protected void onPause() {
pauseScan();
super.onPause();
}
@Override
protected void onDestroy() {
inactivityTimer.shutdown();
if (objectAnimator != null) {
objectAnimator.end();
}
super.onDestroy();
}
/**
* 开始扫码
*/
private void startScan() {
inactivityTimer = new InactivityTimer(this);
beepManager = new BeepManager(this);
// 扫描线动画2(补间动画)
// translateAnimation = 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);
// translateAnimation.setDuration(4500);
// translateAnimation.setRepeatCount(-1);
// translateAnimation.setRepeatMode(Animation.RESTART);
// scanLine.startAnimation(translateAnimation);
if (isPause) {
objectAnimator.resume();
isPause = false;
} else {
objectAnimator.start();
}
// CameraManager must be initialized here, not in onCreate(). This is necessary because we don't
// want to open the camera driver and measure the screen size if we're going to show the help on
// first launch. That led to bugs where the scanning rectangle was the wrong size and partially
// off screen.
cameraManager = new CameraManager(getApplication());
handler = null;
if (isHasSurface) {
// The activity was paused but not stopped, so the surface still exists. Therefore
// surfaceCreated() won't be called, so init the camera here.
initCamera(scanPreview.getHolder());
} else {
// Install the callback and wait for surfaceCreated() to init the camera.
scanPreview.getHolder().addCallback(this);
}
inactivityTimer.onResume();
}
/**
* 暂停扫码
*/
private void pauseScan() {
if (handler != null) {
handler.quitSynchronously();
handler = null;
}
inactivityTimer.onPause();
beepManager.close();
cameraManager.closeDriver();
if (!isHasSurface) {
scanPreview.getHolder().removeCallback(this);
}
objectAnimator.pause();
// translateAnimation.cancel();
isPause = true;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (holder == null) {
Log.e(TAG, "*** WARNING *** surfaceCreated() gave us a null surface!");
}
if (!isHasSurface) {
isHasSurface = true;
initCamera(holder);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
isHasSurface = false;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
/**
* 扫码成功回调方法
* A valid barcode has been found, so give an indication of success and show
* the results.
*
* @param rawResult The contents of the barcode.
* @param bundle The extras
*/
public void handleDecode(Result rawResult, Bundle bundle) {
inactivityTimer.onActivity();
beepManager.playBeepSoundAndVibrate();
//把扫码结果返回到MainActivity
Intent intent = new Intent();
intent.putExtra("barCode", rawResult.getText());
setResult(RESULT_OK, intent);
finish();
//先暂停扫码,然后延时启动扫码
// Toast.makeText(this, rawResult.getText(), Toast.LENGTH_LONG).show();
// pauseScan();
// new Handler().postDelayed(new Runnable() {
// @Override
// public void run() {
// startScan();
// }
// }, 2000);
}
/**
* 初始化相机
*
* @param surfaceHolder
*/
private void initCamera(SurfaceHolder surfaceHolder) {
if (surfaceHolder == null) {
throw new IllegalStateException("No SurfaceHolder provided");
}
if (cameraManager.isOpen()) {
Log.w(TAG, "initCamera() while already open -- late SurfaceView callback?");
return;
}
try {
cameraManager.openDriver(surfaceHolder);
// Creating the handler starts the preview, which can also throw a RuntimeException.
if (handler == null) {
handler = new CaptureActivityHandler(this, cameraManager, DecodeThread.ALL_MODE);
}
initCrop();
} catch (IOException ioe) {
Log.w(TAG, ioe);
displayFrameworkBugMessageAndExit();
} catch (RuntimeException e) {
// Barcode Scanner has seen crashes in the wild of this variety:
// java.?lang.?RuntimeException: Fail to connect to camera service
Log.w(TAG, "Unexpected error initializing camera", e);
displayFrameworkBugMessageAndExit();
}
}
/**
* 相机打开出错弹框
*/
private void displayFrameworkBugMessageAndExit() {
// camera error
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.app_name));
builder.setMessage("相机打开出错,请稍后重试");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
finish();
}
});
builder.show();
}
public void restartPreviewAfterDelay(long delayMS) {
if (handler != null) {
handler.sendEmptyMessageDelayed(R.id.restart_preview, delayMS);
}
}
public Rect getCropRect() {
return mCropRect;
}
/**
* 初始化截取的矩形区域
*/
private void initCrop() {
int cameraWidth = cameraManager.getCameraResolution().y;
int cameraHeight = cameraManager.getCameraResolution().x;
/** 获取布局中扫描框的位置信息 */
int[] location = new int[2];
scanCropView.getLocationInWindow(location);
int cropLeft = location[0];
int cropTop = location[1] - getStatusBarHeight();
int cropWidth = scanCropView.getWidth();
int cropHeight = scanCropView.getHeight();
/** 获取布局容器的宽高 */
int containerWidth = scanContainer.getWidth();
int containerHeight = scanContainer.getHeight();
/** 计算最终截取的矩形的左上角顶点x坐标 */
int x = cropLeft * cameraWidth / containerWidth;
/** 计算最终截取的矩形的左上角顶点y坐标 */
int y = cropTop * cameraHeight / containerHeight;
/** 计算最终截取的矩形的宽度 */
int width = cropWidth * cameraWidth / containerWidth;
/** 计算最终截取的矩形的高度 */
int height = cropHeight * cameraHeight / containerHeight;
/** 生成最终的截取的矩形 */
mCropRect = new Rect(x, y, width + x, height + y);
}
/**
* 获取状态栏高度
*
* @return
*/
private int getStatusBarHeight() {
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
return getResources().getDimensionPixelSize(x);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* dp转px
*
* @param context 上下文
* @param dpValue dp值
* @return px值
*/
public static float dp2px(Context context, float dpValue) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpValue, context.getResources().getDisplayMetrics());
}
/**
* 相册二维码识别、闪光灯部分分界线
****************************************************************************************************/
private static final int SELECT_PIC_KITKAT = 1001;
private static final int SELECT_PIC = 1002;
//打开闪光灯
private void openFlashlight() {
camera = cameraManager.getCamera();
Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
}
//关闭闪光灯
private void closeFlashlight() {
Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.startPreview();
}
/**
* 获得运行时权限
*/
private void getRuntimeRight() {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else {
openAlbum();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openAlbum();
} else {
Toast.makeText(context, "拒绝", Toast.LENGTH_LONG).show();
}
default:
break;
}
}
/**
* 跳转到图片选择
*/
public void openAlbum() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
startActivityForResult(intent, SELECT_PIC_KITKAT);
} else {
startActivityForResult(intent, SELECT_PIC);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PIC && resultCode == Activity.RESULT_OK) { //4.4以下图库
Uri uri = data.getData();
String path = getPath(context, uri);
Result result = scanningImage(path);
if (result == null) {
Toast.makeText(context, "未发现二维码/条形码", Toast.LENGTH_LONG).show();
} else {
// 数据返回
String recode = recode(result.toString());
Toast.makeText(context, recode, Toast.LENGTH_LONG).show();
}
}
//相册返回
if (requestCode == SELECT_PIC_KITKAT && resultCode == Activity.RESULT_OK) { //4.4及以上图库
Uri uri = data.getData();
String path = getPath(context, uri);
Result result = scanningImage(path);
if (result == null) {
Toast.makeText(context, "未发现二维码/条形码", Toast.LENGTH_LONG).show();
} else {
// 数据返回
String recode = recode(result.toString());
Toast.makeText(context, recode, Toast.LENGTH_LONG).show();
}
}
}
/**
* 图片识别
*
* @param path
* @return
*/
protected Result scanningImage(String path) {
if (TextUtils.isEmpty(path)) {
return null;
}
// DecodeHintType 和EncodeHintType
Hashtable<DecodeHintType, String> hints = new Hashtable<>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); // 设置二维码内容的编码
BitmapFactory.Options options = new BitmapFactory.Options();
// options.inJustDecodeBounds = true; // 先获取原大小
options.inJustDecodeBounds = false; // 获取新的大小
int sampleSize = (int) (options.outHeight / (float) 200);
if (sampleSize <= 0)
sampleSize = 1;
options.inSampleSize = sampleSize;
Bitmap scanBitmap = BitmapFactory.decodeFile(path, options);
int[] intArray = new int[scanBitmap.getWidth() * scanBitmap.getHeight()];
scanBitmap.getPixels(intArray, 0, scanBitmap.getWidth(), 0, 0, scanBitmap.getWidth(), scanBitmap.getHeight());
RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap.getWidth(), scanBitmap.getHeight(), intArray);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
try {
return reader.decode(bitmap1, hints);
} catch (NotFoundException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
}
return null;
}
/**
* 中文乱码处理
*
* @param str
* @return
*/
private String recode(String str) {
String formart = "";
try {
boolean ISO = Charset.forName("ISO-8859-1").newEncoder().canEncode(str);
if (ISO) {
formart = new String(str.getBytes("ISO-8859-1"), "GB2312");
} else {
formart = str;
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return formart;
}
/**
* 得到图片路径
*
* @param context
* @param uri
* @return
*/
public String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is Google Photos.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
}
四.去除了ViewfinderView,使用XML布局,自定义扫码界面更灵活简单。
五.github:https://github.com/shenyuanqing/ZxingSimplify
六.参考:
https://github.com/zxing/zxing
https://github.com/chentao0707/ZXingProject
http://blog.youkuaiyun.com/aaawqqq/article/details/24880209