<6> Utils.getPath(getApplicationContext(), data.getData()); //将图片Uri 转换成绝对路径
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());
}
}
- 解析二维码图片
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
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) 方法进行中文乱码处理 代码如下:
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
对于图片识别 有些事项需要注意…
总结
最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上相关的我搜集整理的Android开发中高级必知必会核心笔记,共计2968页PDF、58w字,囊括Android开发648个知识点,我把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节。
网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。
2021年虽然路途坎坷,都在说Android要没落,但是,不要慌,做自己的计划,学自己的习,竞争无处不在,每个行业都是如此。相信自己,没有做不到的,只有想不到的。
虽然面试失败了,但我也不会放弃入职字节跳动的决心的!建议大家面试之前都要有充分的准备,顺顺利利的拿到自己心仪的offer。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
些事项需要注意…
总结
最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上相关的我搜集整理的Android开发中高级必知必会核心笔记,共计2968页PDF、58w字,囊括Android开发648个知识点,我把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节。
[外链图片转存中…(img-Uqypjvun-1715758350733)]
网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。
2021年虽然路途坎坷,都在说Android要没落,但是,不要慌,做自己的计划,学自己的习,竞争无处不在,每个行业都是如此。相信自己,没有做不到的,只有想不到的。
虽然面试失败了,但我也不会放弃入职字节跳动的决心的!建议大家面试之前都要有充分的准备,顺顺利利的拿到自己心仪的offer。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!