Android 使用ZXing库识别图片上的二维码:

1. EncodingHandler.java 文件:

public final class EncodingHandler {private static final int BLACK = 0xff000000;public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();          hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix matrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);int width = matrix.getWidth();int height = matrix.getHeight();int[] pixels = new int[width * height];for (int y = 0; y < height; y++) {for (int x = 0; x < width; x++) {if (matrix.get(x, y)) {pixels[y * width + x] = BLACK;}}}Bitmap bitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, width, 0, 0, width, height);return bitmap;}}

2. RGBLuminanceSource 类继承ZXing库中的LuminanceSource类:

public class RGBLuminanceSource extends LuminanceSource {private final byte[] luminances;private final int dataWidth;private final int dataHeight;private final int left;private final int top;public RGBLuminanceSource(Bitmap bitmap) {super(bitmap.getWidth(), bitmap.getHeight());// TODO Auto-generated constructor stubint width = bitmap.getWidth();int height = bitmap.getHeight();int[] pixels = new int[width * height];bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());dataWidth = width;dataHeight = height;left = 0;top = 0;// In order to measure pure decoding speed, we convert the entire image to a greyscale array// up front, which is the same as the Y channel of the YUVLuminanceSource in the real app.luminances = new byte[width * height];for (int y = 0; y < height; y++) {int offset = y * width;for (int x = 0; x < width; x++) {int pixel = pixels[offset + x];int r = (pixel >> 16) & 0xff;int g = (pixel >> 8) & 0xff;int b = pixel & 0xff;if (r == g && g == b) {// Image is already greyscale, so pick any channel.luminances[offset + x] = (byte) r;} else {// Calculate luminance cheaply, favoring green.luminances[offset + x] = (byte) ((r + g + g + b) >> 2);}}}}@Overridepublic byte[] getMatrix() {// TODO Auto-generated method stubint width = getWidth();int height = getHeight();// If the caller asks for the entire underlying image, save the copy and give them the// original data. The docs specifically warn that result.length must be ignored.if (width == dataWidth && height == dataHeight) {return luminances;}int area = width * height;byte[] matrix = new byte[area];int inputOffset = top * dataWidth + left;// If the width matches the full width of the underlying data, perform a single copy.if (width == dataWidth) {System.arraycopy(luminances, inputOffset, matrix, 0, area);return matrix;}// Otherwise copy one cropped row at a time.byte[] rgb = luminances;for (int y = 0; y < height; y++) {int outputOffset = y * width;System.arraycopy(rgb, inputOffset, matrix, outputOffset, width);inputOffset += dataWidth;}return matrix;}@Overridepublic byte[] getRow(int y, byte[] row) {// TODO Auto-generated method stubif (y < 0 || y >= getHeight()) {throw new IllegalArgumentException("Requested row is outside the image: " + y);}int width = getWidth();if (row == null || row.length < width) {row = new byte[width];}int offset = (y + top) * dataWidth + left;System.arraycopy(luminances, offset, row, 0, width);return row;}}

2,识别图片二维码的代码:

 Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();                hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); // 设置二维码内容的编码                BitmapFactory.Options options = new BitmapFactory.Options();                options.inJustDecodeBounds = true; // 先获取原大小                Bitmap scanBitmap = BitmapFactory.decodeFile("/mnt/sdcard/temp_QRCode.png", options);                options.inJustDecodeBounds = false; // 获取新的大小                int sampleSize = (int) (options.outHeight / (float) 200);                if (sampleSize <= 0)                    sampleSize = 1;                options.inSampleSize = sampleSize;                scanBitmap = BitmapFactory.decodeFile("/mnt/sdcard/temp_QRCode.png", options);                RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap);                BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));                QRCodeReader reader = new QRCodeReader();                try {                    Result result = reader.decode(bitmap1, hints);                    if (result != null) {                        String data = result.toString();                        Toast.makeText(MainActivity.this, "Data= " + data, Toast.LENGTH_LONG).show();                    }                } catch (NotFoundException e) {                    e.printStackTrace();                } catch (ChecksumException e) {                    e.printStackTrace();                } catch (FormatException e) {                    e.printStackTrace();                }

更多相关文章

  1. android图片等比例缩放 填充屏幕
  2. android 使用xml制作背景图片(shape的用法)
  3. 修改android桌面图标默认大小
  4. 2010.11.27———android 展示网络上的图片和播放视频
  5. Android加载图片导致内存溢出(Out of Memory异常)
  6. Android圆角图片
  7. Android:漫画APP开发笔记之ListView中图片按屏幕宽度缩放
  8. Android中获取屏幕相关信息(屏幕大小,状态栏、标题栏高度)
  9. Android中使用Universal-Image-Loader图片缓存

随机推荐

  1. Android Intent 序列化和反序列化
  2. android ui进阶教程
  3. (Android)画个钟看看
  4. Android 开发-Android studio 笔记
  5. Android 子view超出父View效果
  6. GridView层属性
  7. TextView --- 内容设置成上下滑动 和 代
  8. android / ffmpeg dynamic module, JNI s
  9. android之webview使用-处理404等错误
  10. android 数据库操作 GreenDAO 第三方开源