该扫一扫功能能识别二维码,但识别一维码码时扫出来的数据不准确,条码的编码格式是Code128A,但扫一扫功能默认这个条码的格式是EAN13,EAN08
该扫一扫功能能识别二维码,但识别一维码码时扫出来的数据不准确,条码的编码格式是Code128A,但扫一扫功能默认这个条码的格式是EAN13,EAN08
让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言解决这个问题的方法有以下几种:
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.ResultMetadataType;
import com.google.zxing.common.GlobalHistogramBinarizer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.EnumMap;
import java.util.Map;
public class BarcodeReader {
public static void main(String[] args) {
try {
BufferedImage image = ImageIO.read(new File("barcode.jpg"));
LuminanceSource source = new RGBLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.CODE_128);
MultiFormatReader reader = new MultiFormatReader();
Result result = reader.decode(bitmap, hints);
// 获取识别到的数据
String barcodeData = result.getText();
System.out.println("识别到的条码数据为:" + barcodeData);
} catch (IOException | ReaderException e) {
e.printStackTrace();
}
}
}