From e3b04cf2a416e75f2e1b657165b8d57d6a6fa6f1 Mon Sep 17 00:00:00 2001 From: maning Date: Thu, 11 Aug 2016 22:32:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9D=83=E9=99=90=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=AF=E8=83=BD=E5=9B=BE=E5=BA=93=E9=80=89?= =?UTF-8?q?=E6=8B=A9=E5=9B=BE=E7=89=87=E6=97=A0=E6=B3=95=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 5 - libraryzxing/src/main/AndroidManifest.xml | 6 + .../java/com/baozi/Zxing/CaptureActivity.java | 285 ++++-------------- .../com/baozi/Zxing/utils/ZXingUtils.java | 136 +++++++++ 4 files changed, 197 insertions(+), 235 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5d048b9..e42b306 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,11 +2,6 @@ - - - - - + + + + + + hints = new Hashtable(); - 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); - - // --------------测试的解析方法---PlanarYUVLuminanceSource-这几行代码对project没作功---------- - - LuminanceSource source1 = new PlanarYUVLuminanceSource( - rgb2YUV(scanBitmap), scanBitmap.getWidth(), - scanBitmap.getHeight(), 0, 0, scanBitmap.getWidth(), - scanBitmap.getHeight(), false); - BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer( - source1)); - MultiFormatReader reader1 = new MultiFormatReader(); - Result result1; - try { - result1 = reader1.decode(binaryBitmap); - String content = result1.getText(); - Log.i("123content", content); - } catch (NotFoundException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } - - // ---------------------------- - - 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(); - + private void analysisImage(final String photoPath) { + if (TextUtils.isEmpty(photoPath)) { + Toast.makeText(this, "获取图片失败", Toast.LENGTH_SHORT).show(); + return; } - - return null; + new Thread(new Runnable() { + @Override + public void run() { + final String result = ZXingUtils.syncDecodeQRCode(photoPath); + runOnUiThread(new Runnable() { + @Override + public void run() { + if (TextUtils.isEmpty(result)) { + Toast.makeText(CaptureActivity.this, "图片格式错误", Toast.LENGTH_SHORT).show(); + } else { + // 数据返回 + Intent data = new Intent(); + data.putExtra(ZXingConstants.ScanResult, result); + setResult(ZXingConstants.ScanRequestCode, data); + finish(); + } + } + }); + } + }).start(); } @@ -362,9 +259,7 @@ protected void onDestroy() { private void initCamera(SurfaceHolder surfaceHolder) { try { CameraManager.get().openDriver(surfaceHolder); - } catch (IOException ioe) { - return; - } catch (RuntimeException e) { + } catch (Exception ioe) { return; } if (handler == null) { @@ -410,7 +305,7 @@ public void drawViewfinder() { public void handleDecode(final Result result, Bitmap barcode) { inactivityTimer.onActivity(); playBeepSoundAndVibrate(); - String recode = recode(result.toString()); + String recode = ZXingUtils.recode(result.toString()); // 数据返回 Intent data = new Intent(); data.putExtra(ZXingConstants.ScanResult, recode); @@ -463,76 +358,6 @@ public void onCompletion(MediaPlayer mediaPlayer) { } }; - /** - * 中文乱码 - *

- * 暂时解决大部分的中文乱码 但是还有部分的乱码无法解决 . - *

- * 如果您有好的解决方式 请联系 2221673069@qq.com - *

- * 我会很乐意向您请教 谢谢您 - * - * @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"); - 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; - } - - /** - * //TODO: TAOTAO 将bitmap由RGB转换为YUV //TOOD: 研究中 - * - * @param bitmap 转换的图形 - * @return YUV数据 - */ - public byte[] rgb2YUV(Bitmap bitmap) { - // 该方法来自QQ空间 - int width = bitmap.getWidth(); - int height = bitmap.getHeight(); - int[] pixels = new int[width * height]; - bitmap.getPixels(pixels, 0, width, 0, 0, width, height); - - int len = width * height; - byte[] yuv = new byte[len * 3 / 2]; - int y, u, v; - for (int i = 0; i < height; i++) { - for (int j = 0; j < width; j++) { - int rgb = pixels[i * width + j] & 0x00FFFFFF; - - int r = rgb & 0xFF; - int g = (rgb >> 8) & 0xFF; - int b = (rgb >> 16) & 0xFF; - - y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16; - u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128; - v = ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128; - - y = y < 16 ? 16 : (y > 255 ? 255 : y); - u = u < 0 ? 0 : (u > 255 ? 255 : u); - v = v < 0 ? 0 : (v > 255 ? 255 : v); - - yuv[i * width + j] = (byte) y; - // yuv[len + (i >> 1) * width + (j & ~1) + 0] = (byte) u; - // yuv[len + (i >> 1) * width + (j & ~1) + 1] = (byte) v; - } - } - return yuv; - } @Override public void onClick(View v) { diff --git a/libraryzxing/src/main/java/com/baozi/Zxing/utils/ZXingUtils.java b/libraryzxing/src/main/java/com/baozi/Zxing/utils/ZXingUtils.java index 839edb9..28ea01e 100644 --- a/libraryzxing/src/main/java/com/baozi/Zxing/utils/ZXingUtils.java +++ b/libraryzxing/src/main/java/com/baozi/Zxing/utils/ZXingUtils.java @@ -1,16 +1,38 @@ package com.baozi.Zxing.utils; +import android.content.Context; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.graphics.Matrix; +import android.text.TextUtils; +import android.util.Log; +import com.baozi.Zxing.decoding.RGBLuminanceSource; import com.google.zxing.BarcodeFormat; +import com.google.zxing.BinaryBitmap; +import com.google.zxing.ChecksumException; +import com.google.zxing.DecodeHintType; import com.google.zxing.EncodeHintType; +import com.google.zxing.FormatException; +import com.google.zxing.LuminanceSource; +import com.google.zxing.MultiFormatReader; +import com.google.zxing.NotFoundException; +import com.google.zxing.PlanarYUVLuminanceSource; +import com.google.zxing.Result; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; +import com.google.zxing.common.HybridBinarizer; +import com.google.zxing.qrcode.QRCodeReader; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; +import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.EnumMap; import java.util.Hashtable; +import java.util.List; +import java.util.Map; /** * Created by maning on 16/7/27. @@ -18,6 +40,32 @@ */ public class ZXingUtils { + public static final Map HINTS = new EnumMap<>(DecodeHintType.class); + + static { + List 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.POSSIBLE_FORMATS, allFormats); + HINTS.put(DecodeHintType.CHARACTER_SET, "utf-8"); + } + /** * 生成二维码,默认大小为500*500 * @@ -142,4 +190,92 @@ public static Bitmap createQRCodeWithLogo(String text, int size, Bitmap mBitmap) } } + + //------解析图片-----代码来自:https://github.com/bingoogolapple/BGAQRCode-Android----感谢 + /** + * + * 同步解析本地图片二维码。该方法是耗时操作,请在子线程中调用。 + * + * @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) { + try { + int width = bitmap.getWidth(); + int height = bitmap.getHeight(); + int[] pixels = new int[width * height]; + bitmap.getPixels(pixels, 0, width, 0, 0, width, height); + com.google.zxing.RGBLuminanceSource source = new com.google.zxing.RGBLuminanceSource(width, height, pixels); + Result result = new MultiFormatReader().decode(new BinaryBitmap(new HybridBinarizer(source)), HINTS); + if(result != null){ + return recode(result.getText()); + }else{ + return null; + } + } catch (Exception e) { + return null; + } + } + + /** + * 将本地图片文件转换成可解码二维码的 Bitmap + * + * @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; + } + } + + /** + * 中文乱码 + * + * @return + */ + public static 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) { + e.printStackTrace(); + } + return formart; + } + + //--------- + public static int dip2px(Context context,float dpValue) { + final float scale = context.getResources().getDisplayMetrics().density; + return (int) (dpValue * scale + 0.5f); + } + + }