2016-08-14 38 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"; 

默认情况下,如果它被设置为空字符串,它显示的代码文本。

相关问题