当前位置: 代码迷 >> Android >> 请问二维码编码有关问题
  详细解决方案

请问二维码编码有关问题

热度:40   发布时间:2016-05-01 21:51:52.0
请教二维码编码问题
我用下面的代码(用到了zxing中的core.jar和j2se.jar)生成一个二维码图片:
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

public class QREncoder
{
public static void main(String[] args)
{
String content = "酒至颜自解,声和心亦宣。";
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix matrix = null;
try
{
matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300, hints);

catch (WriterException e)
{
e.printStackTrace();
}

File file = new File("D:/qrimage.png");
try
{
MatrixToImageWriter.writeToFile(matrix, "png", file);

catch (IOException e)
{
e.printStackTrace();
}
}
}

自己写了另外一个程序读这个二维码:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class QRDecoder
{
public static void main(String[] args)
{
File file = new File("D:/qrimage.png");
BufferedImage bufferedImage = null;

try
{
bufferedImage = ImageIO.read(file);

catch (IOException e)
{
e.printStackTrace();
}

LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = null;

try
{
result = new MultiFormatReader().decode(bitmap, hints);

catch (NotFoundException e)
{
e.printStackTrace();
}

System.out.println(result.toString());
}
}

可以很好地工作,但是用zxing中的Barcode Scanner居然读不出来自己生成那个二维码,而用另外一个二维码扫描器QuickMark却可以很快地读出来,这是为什么呢?请高手指点。如有满意答案,另外奉送200分,决不食言。

------解决方案--------------------
我自己以前用C++弄过,后来把C++弄的转成Java了,在Android上生成的QR码可以用条码枪或者手机识别,没有问题.你可能是读条码的软件有问题
  相关解决方案