利用ZXing生成二维码
来源:互联网 发布:武汉纬创软件 编辑:程序博客网 时间:2024/05/05 02:47
这里需要用到zxing的core.jar包
// 生成QR图private Bitmap createImage(String text) {try {if (TextUtils.isEmpty(text)) {return null;}QRCodeWriter writer = new QRCodeWriter();BitMatrix martix = writer.encode(text, BarcodeFormat.QR_CODE,QR_WIDTH, QR_HEIGHT);Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");BitMatrix bitMatrix = new QRCodeWriter().encode(text,BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);int[] pixels = new int[QR_WIDTH * QR_HEIGHT];for (int y = 0; y < QR_HEIGHT; y++) {for (int x = 0; x < QR_WIDTH; x++) {if (bitMatrix.get(x, y)) {pixels[y * QR_WIDTH + x] = 0xff000000;} else {pixels[y * QR_WIDTH + x] = 0xffffffff;}}}Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);return bitmap;} catch (WriterException e) {e.printStackTrace();return null;}
core.jar下载地址:点击打开链接
对QRcode图片进行解码:
首先需要用到RGBLuminanceSource这个类;
/* * Copyright 2009 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.google.zxing;/** * This class is used to help decode images from files which arrive as RGB data from * an ARGB pixel array. It does not support rotation. * * @author dswitkin@google.com (Daniel Switkin) * @author Betaminos */public final 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(int width, int height, int[] pixels) { super(width, height); 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); } } } } private RGBLuminanceSource(byte[] pixels, int dataWidth, int dataHeight, int left, int top, int width, int height) { super(width, height); if (left + width > dataWidth || top + height > dataHeight) { throw new IllegalArgumentException("Crop rectangle does not fit within image data."); } this.luminances = pixels; this.dataWidth = dataWidth; this.dataHeight = dataHeight; this.left = left; this.top = top; } @Override public byte[] getRow(int y, byte[] row) { if (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; } @Override public byte[] getMatrix() { int 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; } @Override public boolean isCropSupported() { return true; } @Override public LuminanceSource crop(int left, int top, int width, int height) { return new RGBLuminanceSource(luminances, dataWidth, dataHeight, this.left + left, this.top + top, width, height); }}
解码的方法:
/** * 对QRcode图片进行解码 * * @param bitmap * QRcode图片 * @return 解码后的内容 */public String decodeQRImage(Bitmap bitmap) {String value = null;Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");RGBLuminanceSource source = new RGBLuminanceSource(bitmap);BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));QRCodeReader reader2 = new QRCodeReader();Result result;try {result = reader2.decode(bitmap1, hints);value = result.getText();} catch (NotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ChecksumException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (FormatException e) {// TODO Auto-generated catch blocke.printStackTrace();}return value;}
- 利用ZXing生成二维码
- 利用Zxing生成二维码
- **利用Zxing生成二维码
- Java利用Zxing生成二维码
- Java利用Zxing生成二维码
- Java利用Zxing生成二维码
- Java利用Zxing生成二维码
- Java利用Zxing生成二维码
- Java利用Zxing生成二维码
- Java利用Zxing生成二维码
- Java利用Zxing生成二维码
- Java利用Zxing生成二维码
- Java利用Zxing生成二维码
- Java利用Zxing生成二维码
- Java利用Zxing生成二维码
- Java利用Zxing生成二维码
- Java利用Zxing生成二维码
- java利用zxing生成二维码
- oracle数据库对blob类型数据的存和取
- [Win32]一个调试器的实现(五)调试符号
- U-boot分析(4)
- Three20 iOS 使用
- c++优秀书籍清单
- 利用ZXing生成二维码
- 一步一步mysql (二) 之常见问题
- U-boot分析(5)
- OpenCV简单例子之图形绘画
- 最成功的创业者都是20出头的年轻人?
- [Win32]一个调试器的实现(六)显示源代码
- struts2学习(一)
- 计划任务练习
- 输出字符串的大小写字母的个数!