獲取到閱讀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) { }
}
}
}
另外,爲了獲得更清晰的圖像,您應該使用無損格式,例如PNG而不是JPG。由於圖像的性質,這也可能會生成較小的文件。 –