我正在使用zxing掃描條碼並將其拆分爲pdf。 但是大多數條形碼都被掃描,而且很少沒有。雖然所有條形碼都可以正常顯示,我可以使用條形碼android應用程序掃描它們。 我的代碼是掃描一些條碼但很少不使用Zxing
Boolean flag = Boolean.FALSE;
PDDocument pdfDoc = null;
Result prevResult = null;
try{
pdfDoc = PDDocument.load(new File(pathToReadPdf));
log.debug("Total pdf pages : "+pdfDoc.getNumberOfPages());
Reader reader = new MultiFormatReader();
List<PDPage> pages = pdfDoc.getDocumentCatalog().getAllPages();
for(PDPage page : pages) {
PDResources resources = page.getResources();
// Identify images from pdf
Map images = resources.getImages();
if(images != null){
Iterator imageIter = images.keySet().iterator();
while(imageIter.hasNext()){
String key = (String)imageIter.next();
PDXObjectImage image = (PDXObjectImage)images.get(key);
if (image.getRGBImage()!=null){
Hashtable<DecodeHintType, Object> decodeHints = new Hashtable<DecodeHintType, Object>(3);
Vector<BarcodeFormat> barcodeFormats = new Vector<BarcodeFormat>();
barcodeFormats.add(BarcodeFormat.CODE_128);
decodeHints.put(DecodeHintType.POSSIBLE_FORMATS, barcodeFormats);
decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
decodeHints.put(DecodeHintType.PURE_BARCODE, true);
//decodeHints.put(DecodeHintType.CHARACTER_SET, "ISO-8859-1");
LuminanceSource source = new BufferedImageLuminanceSource(image.getRGBImage(), 0, 0, image.getWidth(), image.getHeight());
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = null;
try{
result = reader.decode(bitmap, decodeHints);
splitPdf(page, result, loanApplicationId);
prevResult= result;
flag = Boolean.TRUE;
}catch(NotFoundException nfe){
if(prevResult!=null){
mergePDF(page, prevResult, loanApplicationId);
}
continue;
}
log.debug("Barcode text is " + result.getText());
}
}
}
}
}catch(Exception e){
e.printStackTrace();
log.error("Error while splitting PDF : " + e.getMessage(), e);
}
finally {
try{
if(pdfDoc != null){
pdfDoc.close();
}
}catch (IOException ioe){
ioe.printStackTrace();
log.error("Error while closing PDF : " + ioe.getMessage(), ioe);
}
}
return flag;`
我認爲錯誤可能是位圖的轉換。 我得到錯誤的com.google.zxing.NotFoundException在result = reader.decode(bitmap, decodeHints);
條碼生成邏輯:
public byte[] createBarCode128(String fileName) {
byte[] imageInByte = new byte[1024];
try {
Code128Bean bean = new Code128Bean();
final int dpi = 300;
//Configure the barcode generator
bean.setModuleWidth(UnitConv.in2mm(6.0f/dpi));
bean.doQuietZone(false);
BitmapCanvasProvider canvas = new BitmapCanvasProvider(null, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
//Generate the barcode
bean.generateBarcode(canvas, fileName);
//Signal end of generation
canvas.finish();
BufferedImage originalImage = canvas.getBufferedImage();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpg", baos);
baos.flush();
imageInByte = baos.toByteArray();
log.debug(imageInByte.toString());
baos.close();
log.debug(" Bar Code is generated successfully ");
}
catch (IOException ex) {
ex.printStackTrace();
log.error(ex.getMessage(),ex);
}
return imageInByte;
}
我使用下面的依賴關係:
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>2.2</version>
</dependency>
我的Java版本6,以斑馬線版本3不受支持。
請提出任何解決方案。
請分享爲提取失敗的樣本PDF。然後我們可以告訴你你的情況出了什麼問題。但一般來說,條形碼不需要使用位圖嵌入。相反,你可能有一個條形碼字體。在這種情況下,條形碼就像普通書寫一樣編碼。我們可以將它們繪製爲矢量圖形。 – mkl 2014-09-24 21:11:49
我已經在這個鏈接https://docs.google.com上分享了pdf的一部分。com/file/d/0B7ZJGCLzY6X6S3VQc183RHgxckk/edit?usp = docslist_api使用我的代碼第一個條形碼是可掃描的,但其他的則不是。 – dev 2014-09-25 04:11:04