Zxing图片识别 从相册选取二维码图片进行解析总结
2016-08-16 18:01
435 查看
Zxing图片识别 从相册选取二维码图片进行解析总结
在Zxing扫描识别和图片识别的解析对象是相同的
本文分三个步骤:
1 获取相册的照片
2 解析二维码图片
3 返回结果
在Activity中开启相册:
[java] view
plain copy
Intent innerIntent = new Intent(); // "android.intent.action.GET_CONTENT"
if (Build.VERSION.SDK_INT < 19) {
innerIntent.setAction(Intent.ACTION_GET_CONTENT);
} else {
// innerIntent.setAction(Intent.ACTION_OPEN_DOCUMENT); 这个方法报 图片地址 空指针;使用下面的方法
innerIntent.setAction(Intent.ACTION_PICK);
}
innerIntent.setType("image/*");
Intent wrapperIntent = Intent.createChooser(innerIntent, "选择二维码图片");
CaptureActivity.this
.startActivityForResult(wrapperIntent, REQUEST_CODE);
选中了照片后返回的数据在onActivityResult方法中获取
[java] view
plain copy
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE:
String[] proj = { MediaStore.Images.Media.DATA };
// 获取选中图片的路径
Cursor cursor = getContentResolver().query(data.getData(),
proj, null, null, null);
if (cursor.moveToFirst()) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
photo_path = cursor.getString(column_index);
if (photo_path == null) {
photo_path = Utils.getPath(getApplicationContext(),
data.getData());
Log.i("123path Utils", photo_path);
}
Log.i("123path", photo_path);
}
cursor.close();
new Thread(new Runnable() {
@Override
public void run() {
Result result = scanningImage(photo_path);
// String result = decode(photo_path);
if (result == null) {
Looper.prepare();
Toast.makeText(getApplicationContext(), "图片格式有误", 0)
.show();
Looper.loop();
} else {
Log.i("123result", result.toString());
// Log.i("123result", result.getText());
// 数据返回
String recode = recode(result.toString());
Intent data = new Intent();
data.putExtra("result", recode);
setResult(300, data);
finish();
}
}
}).start();
break;
}
}
}
上面这段代码
<1> 根据返回的照片信息 获取图片的路径photo_path
<2> 开启一个解析线程调用解析方法Result result = scanningImage(photo_path); 将photo_path传进去
<3> 对返回的解析的Result对象进行判断,获取字符串
<4> 调用recode对result数据进行中文乱码处理 ( 具体在步骤3中说明 )
[java] view
plain copy
String recode = recode(result.toString());
<5>这里我将result 通过setResult(); 返回给了 父Activity
[java] view
plain copy
<6> Utils.getPath(getApplicationContext(), data.getData()); //将图片Uri 转换成绝对路径
[java] view
plain copy
public class Utils {
public static final boolean isChineseCharacter(String chineseStr) {
char[] charArray = chineseStr.toCharArray();
for (int i = 0; i < charArray.length; i++) {
// 是否是Unicode编码,除了"�"这个字符.这个字符要另外处理
if ((charArray[i] >= '\u0000' && charArray[i] < '\uFFFD')
|| ((charArray[i] > '\uFFFD' && charArray[i] < '\uFFFF'))) {
continue;
} else {
return false;
}
}
return true;
}
/**
* Get a file path from a Uri. This will get the the path for Storage Access
* Framework Documents, as well as the _data field for the MediaStore and
* other file-based ContentProviders.
*
* @param context
* The context.
* @param uri
* The Uri to query.
* @author paulburke
*/
public static 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 getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* 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 column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
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());
}
}
plain copy
protected Result scanningImage(String path) {
if (TextUtils.isEmpty(path)) {
return null;
}
// DecodeHintType 和EncodeHintType
Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); // 设置二维码内容的编码
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; // 先获取原大小
scanBitmap = BitmapFactory.decodeFile(path, options);
options.inJustDecodeBounds = false; // 获取新的大小
int sampleSize = (int) (options.outHeight / (float) 200);
if (sampleSize <= 0)
sampleSize = 1;
options.inSampleSize = sampleSize;
scanBitmap = BitmapFactory.decodeFile(path, options);
RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
try {
return reader.decode(bitmap1, hints);
} catch (NotFoundException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
}
return null;
}
首先对result判断是否为空 ,如果为空就代表 二维码不标准或者不是二维码图片
在子线程中使用Toast 需要初始化looper
[java] view
plain copy
Result result = scanningImage(photo_path);
// String result = decode(photo_path);
if (result == null) {
Looper.prepare();
Toast.makeText(getApplicationContext(), "图片格式有误", 0)
.show();
Looper.loop();
} else {
Log.i("123result", result.toString());
// Log.i("123result", result.getText());
// 数据返回
String recode = recode(result.toString());
Intent data = new Intent();
data.putExtra("result", recode);
setResult(300, data);
finish();
}
Result 对象返回的就是二维码扫描的结果
调用recode(result.toString) 方法进行中文乱码处理 代码如下:
[java] view
plain copy
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");
Log.i("1234 ISO8859-1", formart);
} else {
formart = str;
Log.i("1234 stringExtra", str);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return formart;
}
处理好之后将结果字符串 返回给父Activity
对于图片识别 有些事项需要注意..
二维码的图标需要保证图片尽量不倾斜 拍照的时候 最好保证手机与二维码 水平
附图:
希望对您有用 不足之处请指出 谢谢
资源下载地址:https://github.com/ManLikeTheWind/DemoZXing
在Zxing扫描识别和图片识别的解析对象是相同的
本文分三个步骤:
1 获取相册的照片
2 解析二维码图片
3 返回结果
1) 获取相册照片
google对4.4的uri做了点改动 为了适配多种手机 需要做一个判断版本在Activity中开启相册:
[java] view
plain copy
Intent innerIntent = new Intent(); // "android.intent.action.GET_CONTENT"
if (Build.VERSION.SDK_INT < 19) {
innerIntent.setAction(Intent.ACTION_GET_CONTENT);
} else {
// innerIntent.setAction(Intent.ACTION_OPEN_DOCUMENT); 这个方法报 图片地址 空指针;使用下面的方法
innerIntent.setAction(Intent.ACTION_PICK);
}
innerIntent.setType("image/*");
Intent wrapperIntent = Intent.createChooser(innerIntent, "选择二维码图片");
CaptureActivity.this
.startActivityForResult(wrapperIntent, REQUEST_CODE);
选中了照片后返回的数据在onActivityResult方法中获取
[java] view
plain copy
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE:
String[] proj = { MediaStore.Images.Media.DATA };
// 获取选中图片的路径
Cursor cursor = getContentResolver().query(data.getData(),
proj, null, null, null);
if (cursor.moveToFirst()) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
photo_path = cursor.getString(column_index);
if (photo_path == null) {
photo_path = Utils.getPath(getApplicationContext(),
data.getData());
Log.i("123path Utils", photo_path);
}
Log.i("123path", photo_path);
}
cursor.close();
new Thread(new Runnable() {
@Override
public void run() {
Result result = scanningImage(photo_path);
// String result = decode(photo_path);
if (result == null) {
Looper.prepare();
Toast.makeText(getApplicationContext(), "图片格式有误", 0)
.show();
Looper.loop();
} else {
Log.i("123result", result.toString());
// Log.i("123result", result.getText());
// 数据返回
String recode = recode(result.toString());
Intent data = new Intent();
data.putExtra("result", recode);
setResult(300, data);
finish();
}
}
}).start();
break;
}
}
}
上面这段代码
<1> 根据返回的照片信息 获取图片的路径photo_path
<2> 开启一个解析线程调用解析方法Result result = scanningImage(photo_path); 将photo_path传进去
<3> 对返回的解析的Result对象进行判断,获取字符串
<4> 调用recode对result数据进行中文乱码处理 ( 具体在步骤3中说明 )
[java] view
plain copy
String recode = recode(result.toString());
<5>这里我将result 通过setResult(); 返回给了 父Activity
[java] view
plain copy
<6> Utils.getPath(getApplicationContext(), data.getData()); //将图片Uri 转换成绝对路径
[java] view
plain copy
public class Utils {
public static final boolean isChineseCharacter(String chineseStr) {
char[] charArray = chineseStr.toCharArray();
for (int i = 0; i < charArray.length; i++) {
// 是否是Unicode编码,除了"�"这个字符.这个字符要另外处理
if ((charArray[i] >= '\u0000' && charArray[i] < '\uFFFD')
|| ((charArray[i] > '\uFFFD' && charArray[i] < '\uFFFF'))) {
continue;
} else {
return false;
}
}
return true;
}
/**
* Get a file path from a Uri. This will get the the path for Storage Access
* Framework Documents, as well as the _data field for the MediaStore and
* other file-based ContentProviders.
*
* @param context
* The context.
* @param uri
* The Uri to query.
* @author paulburke
*/
public static 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 getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* 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 column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
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());
}
}
2) 解析二维码图片
[java] viewplain copy
protected Result scanningImage(String path) {
if (TextUtils.isEmpty(path)) {
return null;
}
// DecodeHintType 和EncodeHintType
Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); // 设置二维码内容的编码
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; // 先获取原大小
scanBitmap = BitmapFactory.decodeFile(path, options);
options.inJustDecodeBounds = false; // 获取新的大小
int sampleSize = (int) (options.outHeight / (float) 200);
if (sampleSize <= 0)
sampleSize = 1;
options.inSampleSize = sampleSize;
scanBitmap = BitmapFactory.decodeFile(path, options);
RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
try {
return reader.decode(bitmap1, hints);
} catch (NotFoundException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
}
return null;
}
3) 返回结果
首先对result判断是否为空 ,如果为空就代表 二维码不标准或者不是二维码图片在子线程中使用Toast 需要初始化looper
[java] view
plain copy
Result result = scanningImage(photo_path);
// String result = decode(photo_path);
if (result == null) {
Looper.prepare();
Toast.makeText(getApplicationContext(), "图片格式有误", 0)
.show();
Looper.loop();
} else {
Log.i("123result", result.toString());
// Log.i("123result", result.getText());
// 数据返回
String recode = recode(result.toString());
Intent data = new Intent();
data.putExtra("result", recode);
setResult(300, data);
finish();
}
Result 对象返回的就是二维码扫描的结果
调用recode(result.toString) 方法进行中文乱码处理 代码如下:
[java] view
plain copy
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");
Log.i("1234 ISO8859-1", formart);
} else {
formart = str;
Log.i("1234 stringExtra", str);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return formart;
}
处理好之后将结果字符串 返回给父Activity
对于图片识别 有些事项需要注意..
二维码的图标需要保证图片尽量不倾斜 拍照的时候 最好保证手机与二维码 水平
附图:
希望对您有用 不足之处请指出 谢谢
资源下载地址:https://github.com/ManLikeTheWind/DemoZXing
相关文章推荐
- Zxing图片识别 从相册选二维码图片解析总结
- Zxing图片识别 从相册选二维码图片解析总结
- 解决Zxing解析本地图片二维码,识别率低,识别不出来的问题!
- iOS 自带二维码识别(识别二维码,条形码以及从相册中选择图片进行识别)
- Android Zxing 相册二维码/条码图片识别
- 使用Zxing实现拍照,选取图片识别二维码
- Android实现二维码扫描功能(四)-ZXing识别图片二维码,相册选图
- android选取系统相册图片后,识别图中二维码
- zxing扫描二维码和识别图片二维码及其优化策略
- Android开源库之使用ZXing开源库生成二维码及识别本地二维码图片
- iOS 从相册中导入二维码图片识别
- ZXing 生成、解析二维码图片的小示例
- ZXing 相册中识别二维码和条形码(直接引用就可以了)
- Android选取相机、相册图片进行裁剪,并更新UI
- iOS使用ZBar实现二维码扫描以及实现识别相册中二维码图片功能
- iOS设备的摄像头进行拍照,视频。并且从相册中选取我们需要的图片或者视频。
- 使用zxing解析二维码图片
- JAVA 压缩图片-解析一维码二维码-ZXING
- java中ZXing 生成、解析二维码图片的小示例
- ios ZBar识别选中相册的图片二维码