このサードパーティは、私は以前にZXingは、ローカル画像の二次元コードをスキャンする能力を提供見られない二次元コードをスキャンするために使用されるサードパーティを使用ZXingされ、今や次のようにまとめます。
選択したローカル画像のページへジャンプ
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("iamge/*");
startActivityForResult(intent, 300);
その後、コールバックの後に来た画像を選択しonActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
//获取照片数据
Bitmap camera = data.getParcelableExtra("data");
iv1.setImageBitmap(camera);
}
if (requestCode == 200) {
if (data != null) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData()));
iv1.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
if (requestCode == 300) {
String photoPath = getRealFilePath(this, data.getData());
if (photoPath == null) {
LogUtil.fussenLog().d("路径获取失败");
} else {
//解析图片
prasePhoto(photoPath);
}
}
}
私たちは、この場合だけ気に300 requestCodeです
2つのカスタム方法があります:
getRealFilePath方法や分析方法の写真prasePhoto
getRealFilePath方法は、選択した画像の絶対アドレスを取得するために使用されています
導入前の記事では具体的な方法は:URIに応じてファイルパスを取得します。
ダイレクトメソッドからここに掲載しました:
private String getRealFilePath(Context c, Uri uri) {
String result;
Cursor cursor = c.getContentResolver().query(uri,
new String[]{MediaStore.Images.ImageColumns.DATA},//
null, null, null);
if (cursor == null) result = uri.getPath();
else {
cursor.moveToFirst();
int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(index);
cursor.close();
}
return result;
}
実際には、すべての準備作業の前に、次のステップは、主要のコアが経路の機能ZXingれてスキャン画像を使用することです
このメソッドはparsePhotoあるので、時間のかかる必要AsyncTaskまたはRxjavaがここAsyncTaskあるので、方法があります
具体的なコードは次のよう:
private void prasePhoto(final String path) {
AsyncTask myTask = new AsyncTask<String, Integer, String>() {
@Override
protected String doInBackground(String... params) {
// 解析二维码/条码
return QRCodeDecoder.syncDecodeQRCode(path);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (null == s) {
LogUtil.fussenLog().d("图片获取失败,请重试");
} else {
// 识别出图片二维码/条码,内容为s
btn3.setText(s);
LogUtil.fussenLog().d(s);
}
}
}.execute(path);
}
次のように最も重要なコードがあるQRCodeDecoder書かれた自分自身のクラスがあります:
public class QRCodeDecoder {
public static final Map<DecodeHintType, Object> HINTS = new EnumMap<>(DecodeHintType.class);
static {
List<BarcodeFormat> allFormats = new ArrayList<>();
allFormats.add(BarcodeFormat.AZTEC);
allFormats.add(BarcodeFormat.CODABAR);
allFormats.add(BarcodeFormat.CODE_39);
allFormats.add(BarcodeFormat.CODE_93);
allFormats.add(BarcodeFormat.CODE_128);
allFormats.add(BarcodeFormat.DATA_MATRIX);
allFormats.add(BarcodeFormat.EAN_8);
allFormats.add(BarcodeFormat.EAN_13);
allFormats.add(BarcodeFormat.ITF);
allFormats.add(BarcodeFormat.MAXICODE);
allFormats.add(BarcodeFormat.PDF_417);
allFormats.add(BarcodeFormat.QR_CODE);
allFormats.add(BarcodeFormat.RSS_14);
allFormats.add(BarcodeFormat.RSS_EXPANDED);
allFormats.add(BarcodeFormat.UPC_A);
allFormats.add(BarcodeFormat.UPC_E);
allFormats.add(BarcodeFormat.UPC_EAN_EXTENSION);
HINTS.put(DecodeHintType.TRY_HARDER, BarcodeFormat.QR_CODE);
HINTS.put(DecodeHintType.POSSIBLE_FORMATS, allFormats);
HINTS.put(DecodeHintType.CHARACTER_SET, "utf-8");
}
private QRCodeDecoder() {
}
/**
* 同步解析本地图片二维码。该方法是耗时操作,请在子线程中调用。
*
* @param picturePath 要解析的二维码图片本地路径
* @return 返回二维码图片里的内容 或 null
*/
public static String syncDecodeQRCode(String picturePath) {
return syncDecodeQRCode(getDecodeAbleBitmap(picturePath));
}
/**
* 同步解析bitmap二维码。该方法是耗时操作,请在子线程中调用。
*
* @param bitmap 要解析的二维码图片
* @return 返回二维码图片里的内容 或 null
*/
public static String syncDecodeQRCode(Bitmap bitmap) {
Result result = null;
RGBLuminanceSource source = null;
try {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
source = new RGBLuminanceSource(width, height, pixels);
result = new MultiFormatReader().decode(new BinaryBitmap(new HybridBinarizer(source)), HINTS);
return result.getText();
} catch (Exception e) {
e.printStackTrace();
if (source != null) {
try {
result = new MultiFormatReader().decode(new BinaryBitmap(new GlobalHistogramBinarizer(source)), HINTS);
return result.getText();
} catch (Throwable e2) {
e2.printStackTrace();
}
}
return null;
}
}
/**
* 将本地图片文件转换成可解码二维码的 Bitmap。为了避免图片太大,这里对图片进行了压缩。感谢 https://github.com/devilsen 提的 PR
*
* @param picturePath 本地图片文件路径
* @return
*/
private static Bitmap getDecodeAbleBitmap(String picturePath) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(picturePath, options);
int sampleSize = options.outHeight / 400;
if (sampleSize <= 0) {
sampleSize = 1;
}
options.inSampleSize = sampleSize;
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(picturePath, options);
} catch (Exception e) {
return null;
}
}
}
これらのツールに編成コードの最も重要な記事です
三つの方法の内側には、まず、着信画像の絶対アドレスは、ビットマップに実際のビットマップであり、その後、元の内側をzxingで2次元コードのスキャン画像の結果を読み、次に、実際には、唯一の最も呼び出しそれを統合するために戻りますそのsyncDecodeQRCodeその上、この方法上記。
-------------------------------------------------- -------------------------------------------------- ------------
Psが、その検討事項のいくつかを考慮することを忘れないでください、これは時間のかかる作業であることを忘れないでください。