问题描述
- java利用zxing包解二维码
-
我在网上找了一个解二维码的代码,利用的zxing包的功能解码,代码和zxing包都是现成的,编译通过然后运行是有些二维码能解析出来,有些就解析不出来。求大神
package test;import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.EnumMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.BufferedImageLuminanceSource;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Reader;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
public class ZxingPdfRead {
private static Reader barcodeReader = new MultiFormatReader();
/** * @param args * @throws IOException */ public static void main(String[] args) throws Exception { File testImage = new File( "E:\work\all_workspace\wp_zxing\barcode4jTest\src\test\helloworld.png"); BufferedImage image = ImageIO.read(testImage); LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); try { Map<DecodeHintType, Object> hints = new EnumMap<DecodeHintType, Object>( DecodeHintType.class); hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); Result result = barcodeReader.decode(bitmap, hints); String resultText = result.getText(); System.out.println("resultText:" + URLDecoder.decode(resultText, "UTF-8")); } catch (ReaderException ignored) { ignored.printStackTrace(); } }
}
解决方案
解析不出来很正常,图片解析度太低,编码不标准,拍照没有对焦,过于偏斜等等都会识别不出来。
解决方案二:
图片解析度没问题,手机都能解析出来,而且我是直接用的电脑生成的jpg二维码,不是扫出来保存的。图片不存在问题的
时间: 2024-09-17 04:33:06