如何从条形码图像中删除文本

时间:2016-08-14 17:58:49

标签: java barcode

我编写的代码可以生成与 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();
    }
}

3 个答案:

答案 0 :(得分: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) { }
        }
    }
}

答案 1 :(得分:0)

设置此属性:

builder.CodeLocation = CodeLocation.None; 
  

将CodeLocation设置为None将隐藏由此创建的条形码文本   条形码的效果,没有任何价值。

来自Aspose支持论坛:https://forum.aspose.com/t/creating-2d-bar-code-using-aspose-and-merging-it-in-pdf/7265

如果您想更改标题文字,请使用此属性...

builder.Display2DText = "this is caption text";

默认情况下,如果将其设置为空字符串,则会显示codetext。

答案 2 :(得分:0)

在下面的链接中阅读条形码方法的属性 http://barbecue.sourceforge.net/apidocs/net/sourceforge/barbecue/Barcode.html

使用 setDrawingText (布尔值)默认方法,我们删除下面条形码中的文本

示例:

Barcode barcode = BarcodeFactory.createCode128(id);
barcode.setDrawingText(false);
BarcodeImageHandler.writePNG(barcode, new FileOutputStream(new File(file Name)));