2016-08-14 76 views
0

我已經寫了一個代碼,可以生成相對於的條形碼String codeText =「1104006」;,也可以讀取該條形碼的數據。但問題是,在條形碼下方生成條形碼時,它還會寫入文字(codeText)。我如何刪除可讀的文本,在例子中用紅色圈起來?如何從條形碼圖像中刪除文本

example output with human readable circled in red

public class Main { 

    private static String strBarFolder = ("C:\\Users\\Jobayer__\\Desktop\\"); 

    public static void main(String[] args) { 

     String codeText = "1104006"; 
     String strImageFile = ("barcode.jpg"); 

     BarCodeBuilder builder = new BarCodeBuilder(Symbology.CODE39STANDARD, codeText); 
     builder.save(strBarFolder + strImageFile); 
     System.out.println("Successfully Done"); 

     Image img = Toolkit.getDefaultToolkit().getImage(strBarFolder + strImageFile); 
     BarCodeReader reader = new BarCodeReader(img, BarCodeReadType.Code39Standard); 

     while(reader.read()){ 
      System.out.println("Code Text Found: " + reader.getCodeText()); 
     } 
     reader.close(); 
    } 
} 
+0

另外,爲了獲得更清晰的圖像,您應該使用無損格式,例如PNG而不是JPG。由於圖像的性質,這也可能會生成較小的文件。 –

回答

0

獲取到閱讀Aspose源代碼(如果它是允許的),並註釋掉drawString之()方法,其繪製文本到影像,也可以修改條形碼圖像在圖像的文本區域上繪製一個帶有fillRect()的白色矩形。

下面是一個小的可運行的控制檯應用程序,我很快就執行了後者。它是基於關閉您提供的條碼圖像:

package barcodetextcoverup; 

import java.awt.Color; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import java.awt.image.RenderedImage; 
import java.io.File; 
import java.io.IOException; 
import java.util.Scanner; 
import javax.imageio.ImageIO; 

public class BarCodeTextCoverUp { 

    public static void main(String[] args) { 
     startTextCover(); 
    } 

    private static void startTextCover() { 
     Scanner scnr = new Scanner(System.in); 
     String userInput = ""; 
     while (!userInput.equalsIgnoreCase("quit")) { 
      System.out.println("\nEnter the path and file name to the Bar Code image file\n" 
          + "to modify or enter quit to exit:"); 
      userInput = scnr.nextLine(); 
      if (userInput.equalsIgnoreCase("quit")) { break; } 

      // opening a bar code image from disk 
      BufferedImage src = null; 
      try { 
       src = ImageIO.read(new File(userInput)); 
       int iWidth = src.getWidth(); 
       int iHeight = src.getHeight(); 
       // Modify the image... 
       Graphics2D g2 = src.createGraphics(); 
       g2.setColor(Color.WHITE); 
       //Cover the text: Aspose.Demo 
       g2.fillRect(0, 0, 150, 30); 
       // Cover the codeText at bottom of Bar Code 
       g2.fillRect((iWidth/2) - 75, iHeight - 40, 150, 35); 
       g2.dispose(); 

       System.out.println("\nEnter a NEW path and file name for the modified Bar Code image.\n" 
           + "You can use the same file name just change the extention to .png:"); 
       userInput = scnr.nextLine(); 
       // If nothing is supplied then the modifications are not saved. 
       if (!userInput.equals("")) { 
        ImageIO.write((RenderedImage) src, "PNG", new File(userInput)); 
       } 
       System.out.println("----------------------------------------------------------------------"); 
      } catch (IOException ex) { } 
     } 
    } 
} 
0

設置該屬性:

builder.CodeLocation = CodeLocation.None; 

設置CodeLocation爲無會隱藏從而創造 條碼沒有任何效果的條形碼文本它的價值。

來自Aspose支持論壇:https://forum.aspose.com/t/creating-2d-bar-code-using-aspose-and-merging-it-in-pdf/7265如果你想更改字幕文本使用這個屬性

...

builder.Display2DText = "this is caption text"; 

默認情況下,如果它被設置爲空字符串,它顯示的代碼文本。

相關問題