zxing识别图片二维码

图片处理

[java]  view plain  copy
  1. import android.graphics.Bitmap;  
  2. import android.graphics.Canvas;  
  3. import android.graphics.Color;  
  4. import android.graphics.Paint;  
  5. import android.graphics.drawable.Drawable;  
  6.   
  7. public class ImageUtils {     
  8.     /** 
  9.      * drawableToBitmap with white background 
  10.      * @param drawable 
  11.      * @return 
  12.      */  
  13.     public static Bitmap drawableToBitmap(Drawable drawable) {  
  14.         // 取 drawable 的长宽  
  15.         int w = drawable.getIntrinsicWidth();  
  16.         int h = drawable.getIntrinsicHeight();  
  17.           
  18.         // 取 drawable 的颜色格式  
  19.         Bitmap.Config config = Bitmap.Config.ARGB_8888;  
  20.         // 建立对应 bitmap  
  21.         Bitmap bitmap = Bitmap.createBitmap(w, h, config);  
  22.         // 建立对应 bitmap 的画布  
  23.         Canvas canvas = new Canvas(bitmap);  
  24.         // 白色底色   应对透明图  
  25.         Paint paint = new Paint();  
  26.         paint.setColor(Color.WHITE);  
  27.         canvas.drawRect(00, w, h, paint);  
  28.         drawable.setBounds(00, w, h);  
  29.         // 把 drawable 内容画到画布中  
  30.         drawable.draw(canvas);  
  31.         return bitmap;  
  32.     }  
  33.       
  34.     /** 
  35.      * YUV420sp 
  36.      *  
  37.      * @param inputWidth 
  38.      * @param inputHeight 
  39.      * @param scaled 
  40.      * @return 
  41.      */  
  42.     public static byte[] getYUV420sp(int inputWidth, int inputHeight,  
  43.             Bitmap scaled) {  
  44.         int[] argb = new int[inputWidth * inputHeight];  
  45.   
  46.         scaled.getPixels(argb, 0, inputWidth, 00, inputWidth, inputHeight);  
  47.   
  48.         byte[] yuv = new byte[inputWidth * inputHeight * 3 / 2];  
  49.           
  50.         encodeYUV420SP(yuv, argb, inputWidth, inputHeight);  
  51.   
  52.         scaled.recycle();  
  53.   
  54.         return yuv;  
  55.     }  
  56.   
  57.     /** 
  58.      * RGB转YUV420sp 
  59.      *  
  60.      * @param yuv420sp 
  61.      *            inputWidth * inputHeight * 3 / 2 
  62.      * @param argb 
  63.      *            inputWidth * inputHeight 
  64.      * @param width 
  65.      * @param height 
  66.      */  
  67.     private static void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width,  
  68.             int height) {  
  69.         // 帧图片的像素大小  
  70.         final int frameSize = width * height;  
  71.         // ---YUV数据---  
  72.         int Y, U, V;  
  73.         // Y的index从0开始  
  74.         int yIndex = 0;  
  75.         // UV的index从frameSize开始  
  76.         int uvIndex = frameSize;  
  77.   
  78.         // ---颜色数据---  
  79. //      int a, R, G, B;  
  80.         int R, G, B;  
  81.         //  
  82.         int argbIndex = 0;  
  83.         //  
  84.   
  85.         // ---循环所有像素点,RGB转YUV---  
  86.         for (int j = 0; j < height; j++) {  
  87.             for (int i = 0; i < width; i++) {  
  88.   
  89.                 // a is not used obviously  
  90. //              a = (argb[argbIndex] & 0xff000000) >> 24;  
  91.                 R = (argb[argbIndex] & 0xff0000) >> 16;  
  92.                 G = (argb[argbIndex] & 0xff00) >> 8;  
  93.                 B = (argb[argbIndex] & 0xff);  
  94.                 //  
  95.                 argbIndex++;  
  96.   
  97.                 // well known RGB to YUV algorithm  
  98.                 Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;  
  99.                 U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;  
  100.                 V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;  
  101.   
  102.                 //  
  103.                 Y = Math.max(0, Math.min(Y, 255));  
  104.                 U = Math.max(0, Math.min(U, 255));  
  105.                 V = Math.max(0, Math.min(V, 255));  
  106.   
  107.                 // NV21 has a plane of Y and interleaved planes of VU each  
  108.                 // sampled by a factor of 2  
  109.                 // meaning for every 4 Y pixels there are 1 V and 1 U. Note the  
  110.                 // sampling is every other  
  111.                 // pixel AND every other scanline.  
  112.                 // ---Y---  
  113.                 yuv420sp[yIndex++] = (byte) Y;  
  114.                 // ---UV---  
  115.                 if ((j % 2 == 0) && (i % 2 == 0)) {  
  116.                     //  
  117.                     yuv420sp[uvIndex++] = (byte) V;  
  118.                     //  
  119.                     yuv420sp[uvIndex++] = (byte) U;  
  120.                 }  
  121.             }  
  122.         }  
  123.     }  
  124. }  



二维码读取,bitmap-->yuv数据--->解析

[java]  view plain  copy
  1. import java.util.Hashtable;  
  2.   
  3. import android.graphics.Bitmap;  
  4. import android.graphics.drawable.Drawable;  
  5.   
  6. import com.google.zxing.BarcodeFormat;  
  7. import com.google.zxing.BinaryBitmap;  
  8. import com.google.zxing.DecodeHintType;  
  9. import com.google.zxing.PlanarYUVLuminanceSource;  
  10. import com.google.zxing.Result;  
  11. import com.google.zxing.common.HybridBinarizer;  
  12. import com.google.zxing.qrcode.QRCodeReader;  
  13.   
  14. public class QRCodeUtils {  
  15.   
  16.     public static String getStringFromQRCode(Drawable drawable) {  
  17.         String httpString = null;  
  18.           
  19.         Bitmap bmp = ImageUtils.drawableToBitmap(drawable);  
  20.         byte[] data = ImageUtils.getYUV420sp(bmp.getWidth(), bmp.getHeight(), bmp);  
  21.         // 处理  
  22.         try {  
  23.             Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();  
  24. //            hints.put(DecodeHintType.CHARACTER_SET, "utf-8");  
  25.             hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);  
  26.             hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);  
  27.             PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data,   
  28.                     bmp.getWidth(),   
  29.                     bmp.getHeight(),  
  30.                     00,  
  31.                     bmp.getWidth(),   
  32.                     bmp.getHeight(),  
  33.                     false);  
  34.             BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));   
  35.             QRCodeReader reader2= new QRCodeReader();  
  36.             Result result = reader2.decode(bitmap1, hints);  
  37.   
  38.             httpString = result.getText();  
  39.         } catch (Exception e) {  
  40.             e.printStackTrace();  
  41.         }   
  42.           
  43.         bmp.recycle();  
  44.         bmp = null;  
  45.           
  46.         return httpString;  
  47.     }  
  48. }  



调用示例

[java]  view plain  copy
  1. String http = QRCodeUtils.getStringFromQRCode(drawable);  

猜你喜欢

转载自blog.csdn.net/guojinyu_001/article/details/79817533
今日推荐