添加所需要的依赖框架:
implementation 'cn.yipianfengye.android:zxing-library:2.2'
implementation 'com.github.bumptech.glide:glide:4.1.1'
长按识别图片是否为二维码:
/**
* 长按webview图片
*/
webView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
final WebView.HitTestResult htr = ((WebView) v).getHitTestResult();//获取所点击的内容
if (htr.getType() == WebView.HitTestResult.IMAGE_TYPE //判断被点击的类型为图片
|| htr.getType() == WebView.HitTestResult.IMAGE_ANCHOR_TYPE
|| htr.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
imagePath = htr.getExtra();
analyzeBitmap(htr.getExtra(), new AnalyzeCallback() {
@Override
public void onAnalyzeSuccess(Bitmap mBitmap, final String result) {
List<String> list = new ArrayList<>();
list.add("保存图片到本地");
list.add("识别图中二维码");
showDialog(list, result);
}
@Override
public void onAnalyzeFailed() {
List<String> list = new ArrayList<>();
list.add("保存图片到本地");
showDialog(list, "");
}
});
}
return false;
}
});
/**
* 解析二维码图片工具类
*/
public void analyzeBitmap(final String path, final AnalyzeCallback analyzeCallback) {
Glide.with(getActivity()).load(path).into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
BitmapDrawable bd = (BitmapDrawable) resource;
mBitmap = bd.getBitmap();
MultiFormatReader multiFormatReader = new MultiFormatReader();
// 解码的参数
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(2);
// 可以解析的编码类型
Vector<BarcodeFormat> decodeFormats = new Vector<BarcodeFormat>();
if (decodeFormats == null || decodeFormats.isEmpty()) {
decodeFormats = new Vector<BarcodeFormat>();
// 这里设置可扫描的类型,我这里选择了都支持
decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS);
decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS);
decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS);
}
hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
// 设置继续的字符编码格式为UTF8
// hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
// 设置解析配置参数
multiFormatReader.setHints(hints);
// 开始对图像资源解码
Result rawResult = null;
try {
rawResult = multiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(mBitmap))));
} catch (Exception e) {
e.printStackTrace();
}
if (rawResult != null) {
if (analyzeCallback != null) {
analyzeCallback.onAnalyzeSuccess(mBitmap, rawResult.getText());
}
} else {
if (analyzeCallback != null) {
analyzeCallback.onAnalyzeFailed();
}
}
}
});
}
/**
* 解析二维码结果
*/
public interface AnalyzeCallback {
void onAnalyzeSuccess(Bitmap mBitmap, String result);
void onAnalyzeFailed();
}
附:保存图片到本地的方法:
private class SaveImage extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String result = "";
try {
String sdcard = null;
String sdStatus = Environment.getExternalStorageState();
if (sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd卡是否可用
sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
} else {
ToastUtil.show(getActivity(), "SD卡不可用");
}
File file = new File(sdcard + "/Download");
if (!file.exists()) {
file.mkdirs();
}
file = new File(sdcard + "/Download/" + new Date().getTime() + ".jpg");
InputStream inputStream = null;
URL url = new URL(imagePath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(20000);
if (conn.getResponseCode() == 200) {
inputStream = conn.getInputStream();
}
byte[] buffer = new byte[4096];
int len = 0;
FileOutputStream outStream = new FileOutputStream(file);
while ((len = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
outStream.close();
result = "图片已保存至:" + file.getAbsolutePath();
} catch (Exception e) {
result = "保存失败!" + e.getLocalizedMessage();
}
return result;
}
@Override
protected void onPostExecute(String result) {
ToastUtil.show(getActivity(), result);
}
}
使用方法:
SaveImage saveImage = new SaveImage();
saveImage.execute();
注:Android 6.0以上需动态申请权限:WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE,才能保存图片到本地。
网友评论