我已經分別通過插入2種類型條形碼QRCode和Code128來創建PDF。使用iText從PDF條形碼圖像中檢索文本
現在我的問題是如何提取條形碼圖像第一次和第二次從這些圖像的文字, 請幫我對此,我嘗試和谷歌2天,但沒有找到合適的方式。
那麼我使用的是itextpdf-5.5.8版本。
我已經分別通過插入2種類型條形碼QRCode和Code128來創建PDF。使用iText從PDF條形碼圖像中檢索文本
現在我的問題是如何提取條形碼圖像第一次和第二次從這些圖像的文字, 請幫我對此,我嘗試和谷歌2天,但沒有找到合適的方式。
那麼我使用的是itextpdf-5.5.8版本。
那麼像以前我評論了關於一招爲Code128 Barcode Image
沒有得到鼠標點擊PDF選擇,爲QR碼。
所以,我的Code128 Barcode
圖像作爲File
存儲在一個文件夾中TMP,後來我插入這些圖片從文件中,這樣做,我得到了Barcode Image
點擊鼠標完成圖像的javaxt.io.Image
API的幫助選擇。
下面是代碼如何插入Code128 Barcode Image
在PDF
時間 -
private static void insertBAR(PdfContentByte cb, PdfPTable table, String text, int colspan, com.itextpdf.text.Font font){
Barcode128 code128 = new Barcode128();
code128.setBaseline(-1);
code128.setSize(16f);
//code128.setBarHeight(16f);
code128.setCode(text.trim());
code128.setCodeType(Barcode128.CODE128);
Image code128Image = code128.createImageWithBarcode(cb, new BaseColor(0, 47, 47), null);
java.awt.Image awtImage = code128.createAwtImage(new Color(0, 47, 47), Color.WHITE);
//java.awt.Image awtImage = code128.createAwtImage(Color.BLACK, Color.WHITE);
//Initialising a 6+ (width and height)size of BufferedImage to put the Barcode Image in center
BufferedImage bi = new BufferedImage(awtImage.getWidth(null)+6, awtImage.getHeight(null)+6, BufferedImage.TYPE_INT_ARGB);
Graphics2D gd = bi.createGraphics();
gd.setColor(Color.WHITE);
gd.fillRect(0, 0, bi.getWidth(), bi.getHeight());
//drawing the Barcode Image in center of BufferedImage Rect.
gd.drawImage(awtImage, 3, 3, null);
gd.dispose();
File imgFile = new File(OneMethod.getElementosPath() + "/db/tmp/Img" + (++shortCount) + ".png");
try {
javaxt.io.Image img = new javaxt.io.Image(bi);
img.saveAs(imgFile);
code128Image = Image.getInstance(imgFile.getAbsolutePath());
} catch (BadElementException | IOException ex) {
ex.printStackTrace();
}
PdfPCell cell = new PdfPCell();
cell.setMinimumHeight(35f);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(2f);
cell.setColspan(colspan);
cell.addElement(code128Image);
//cell.setImage(code128Image);
table.addCell(cell);
}
,現在我該如何首先Barcode Image
使用iText
,然後再使用zxing
API檢索這些圖像的String
值提取 -
private void readAndValidateContents(){
if(externalLOB.size() == 2){
try {
PdfReader reader = new PdfReader(externalLOB.get(0).getAbsolutePath());
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
//Trying to Extract Images to a tmp folder
ImageRenderListener listener = new ImageRenderListener(OneMethod.getElementosPath() + "/db/tmp/Img%s.%s");
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
parser.processContent(i, listener);
}
//Retrieving String values from images extracted to tmp folder
ArrayList<String> ary = OneMethod.decodeBarcodes();
//Extracting String values from PDF Page using iText API
String[] contents = (PdfTextExtractor.getTextFromPage(reader, 1)).split("\n");
externalKey.clear();
System.out.println("\n\n");
short line = 0;
for(String str : contents){
System.out.println(line++ + str);
}
String tempVal = ary.get(0);
String[] split = tempVal.split("\n");
tempVal = split[0];
tempVal = tempVal.substring(0, tempVal.indexOf("."));
System.out.println("\n\n" + tempVal + "\n\n");
if(contents[4].equals(tempVal)){ //comparing qr bar code file name with pdf text file name
System.out.println("Something XYZ....Here...Done...")
}else{
System.err.println("Possibilities for File Interruption, File different of requested...");
}
} catch (StringIndexOutOfBoundsException spiobe){
System.err.println("File different of requested...");
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception ex){
ex.printStackTrace();
} finally{
OneMethod.clearDirectory(OneMethod.getElementosPath() + "/db/tmp/");
}
}
}
class OneMethod {
static synchronized ArrayList<String> decodeBarcodes(){
ArrayList<String> ary = new ArrayList<String>();
short suffix = 1;
File imageFile = null;
outer:
while(true){
if(++suffix > 5) //Starting to read the Img with Suffix 2 i.e. Img2.jpg
break outer;
inner:
while(true){
try{
InputStream barCodeInputStream;
imageFile = new File(OneMethod.getElementosPath()
+ "/db/tmp/Img" + suffix + (suffix != 2 ? ".png" : ".jpg"));
if(imageFile.exists() && imageFile.isFile()){
barCodeInputStream = new FileInputStream(imageFile.getAbsolutePath());
BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);
//Using zxing-core-3.2.0 with collaborating the old 1.3 version com.google.zxing.client.j2se package inside.
//So doing above action you will get the BufferedImageLuminanceSource class which is now not available in new versions.
LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
Result result = reader.decode(bitmap);
System.out.println(suffix + " value is : " + result.getText());
ary.add(result.getText());
}else{
System.out.println(imageFile.getAbsolutePath() + " path is not available...");
}
break inner;
} catch(FileNotFoundException | NullPointerException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (NotFoundException | ChecksumException | FormatException ex) {
ex.printStackTrace();
}
}
}
return ary;
}
static synchronized void clearDirectory(final String dirPath){
new Thread(new Runnable(){
public void run(){
try{
File directory = new File(dirPath);
if(directory.isDirectory() == true)
try {
File[] files = directory.listFiles();
for(File file : files){
FileUtils.deleteQuietly(file);
Thread.sleep(100);
if(file.exists())
FileUtils.forceDeleteOnExit(file);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}catch(InterruptedException exp){
exp.printStackTrace();
}
}
}).start();
}
}
class ImageRenderListener implements RenderListener {
/**
* Creates a RenderListener that will look for images.
*/
public ImageRenderListener(String path) {
this.path = path;
tempCount = 0;
}
/**
* @see com.itextpdf.text.pdf.parser.RenderListener#beginTextBlock()
*/
public void beginTextBlock() {}
/**
* @see com.itextpdf.text.pdf.parser.RenderListener#endTextBlock()
*/
public void endTextBlock() {}
/**
* @see com.itextpdf.text.pdf.parser.RenderListener#renderImage(
* com.itextpdf.text.pdf.parser.ImageRenderInfo)
*/
public void renderImage(ImageRenderInfo renderInfo) {
//public void renderImage(InlineImageInfo renderInfo) {
try {
String filename;
FileOutputStream os;
PdfImageObject image = renderInfo.getImage();
if (image == null) return;
filename = String.format(path, ++tempCount, image.getFileType());
os = new FileOutputStream(filename);
os.write(image.getImageAsBytes());
os.flush();
os.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
/**
* @see com.itextpdf.text.pdf.parser.RenderListener#renderText(
* com.itextpdf.text.pdf.parser.TextRenderInfo)
*/
public void renderText(TextRenderInfo renderInfo) {}
}
我也在研究一個類似的項目,但它的.NET。我爲第二個問題提供了一個可選解決方案,即從圖像中讀取條形碼。您可以使用ZXing解碼條碼。下面是僞代碼,你必須通過在函數圖像路徑:
static void ScanBarCode(string FileName) {
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(FileName);
try {
BarcodeReader reader = new BarcodeReader { AutoRotate = true, TryHarder = true };
Result result = reader.Decode(bitmap);
string decodedData = result.Text;
Console.WriteLine(result.ToString());
} catch {
throw new Exception("Cannot decode the Barcode code");
}
}
我知道從PDF中提取圖像的一種方法,但是在代碼和新技巧中尋找短小的東西,這是我的第一個問題,看到3年之前,我也是maki ng項目在VB.Net上,當我看.Net時,我的臉上露出了一絲笑容,讓我試試看你的建議...... – ArifMustafa
Lemme也知道,我也面臨同樣的挑戰, PDF轉換爲Byte或.PNG,以便我可以檢索條碼值。 –
您可以使用iText來提取圖像,然後將圖像數據提供給ZXing進行解碼。順便說一句,iText也使用ZXing,但僅限於編碼。 –