我用下面的代码(用到了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分,决不食言。

解决方案 »

  1.   

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

  2.   


    谢谢回复。问题是读QR码的软件,用了zxing中的barcode scanner,按理应该不会有问题,可是事情摆在面前,它的确无法正确读取自己生成的QR码,而QuickMark却可以,而且是很快地读出来。各位耍过的朋友,快来帮帮哈。
      

  3.   

    看来是木有人来回答这个问题了,很失望。我在CSDN上所有不满意结贴都发生在Android版块。
      

  4.   

    昨天俺就搞定了,今天解决了很多人都在问的中文乱码的问题。准备写一篇博客,给准备做这方面工作的朋友借鉴。下面是俺自己做的一个标记,嘿嘿。
    <<<Encoder DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1";
      

  5.   


    zxing c++ 版本没有 生成的, 只有读的吗
      

  6.   

    zxing c++ 版本没有 生成的, 只有读的吗
      

  7.   

    bufferedimge 在安卓项目 中不可用吧