尝试将文本添加到多页(多图像)tiff文件中。
使正确添加文本的过程,但创建的图像不等于内容更改的原始缩放(比例)。我不知道如何使它等于原始图像。
这是我使用的代码:
File file = new File("AA005E57.tif");
SeekableStream seekableStream = new FileSeekableStream(file);
BufferedImage bsrc = ImageIO.read(file);
ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", seekableStream, null);
int numPages = decoder.getNumPages();
BufferedImage image[]= new BufferedImage[numPages];
for(int i=0;i<decoder.getNumPages();i++){
PlanarImage op1 = new NullOpImage(decoder.decodeAsRenderedImage(i), null, null, OpImage.OP_IO_BOUND);
BufferedImage pg1 = convertRenderedImage(op1);// (new BufferedImage(op1.getWidth(), op1.getHeight(),BufferedImage.TYPE_BYTE_BINARY));
image[i] = pg1;
Graphics2D g2 = image[i].createGraphics();
float pageWidthInch = image[i].getWidth() * 72 / 200;
float pageHeightInch = image[i].getHeight() * 72 / 100;
g2.scale(pageWidthInch, pageHeightInch);
if (i == 0 ){
Font font = new Font("Helvetica", Font.BOLD, 12);
g2.setColor(Color.black);
g2.setFont(font);
g2.drawString("RADICADO 1234567890214365-D", 25,pg1.getHeight()-25);
}
g2.drawImage(image[i], (image[i].getWidth()),image[i].getHeight() ,null);
g2.dispose();
}
save(image,"C:/Prueb-18.tif");
convertRenderedImage
的代码:
if (img instanceof BufferedImage) {
return (BufferedImage)img;
}
ColorModel cm = img.getColorModel();
float pageWidthInch = img.getWidth() * 72 / 200;
float pageHeightInch = img.getHeight() * 72 / 100;
WritableRaster raster = cm.createCompatibleWritableRaster( (int)pageWidthInch,(int)pageHeightInch);
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
Hashtable properties = new Hashtable();
String[] keys = img.getPropertyNames();
if (keys!=null) {
for (int i = 0; i < keys.length; i++) {
properties.put(keys[i], img.getProperty(keys[i]));
}
}
BufferedImage result = new BufferedImage(cm, raster, false, properties);
img.copyData(raster);
return result;
保存方法代码:
Iterator writers = ImageIO.getImageWritersByFormatName("TIFF");
if (writers == null || !writers.hasNext()) {
throw new RuntimeException("No writers are available.");
}
FileImageOutputStream fios = new FileImageOutputStream(new File(tif));
ImageWriter writer = (ImageWriter) writers.next();
writer.setOutput(fios);
writer.prepareWriteSequence(null);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("CCITT T.4");
for (int i = 0; i < b.length; i++) {
ImageTypeSpecifier imageType = ImageTypeSpecifier.createFromRenderedImage(b[i]);
IIOMetadata imageMetadata = writer.getDefaultImageMetadata(imageType, param);
imageMetadata = createImageMetadata(imageMetadata);
writer.writeToSequence(new IIOImage(b[i], null, imageMetadata),param);
}
writer.endWriteSequence();
writer.dispose();
writer = null;
fios.close();
}
createImageMetadata代码:
char[] COMPRESSION = new char[] { (char) BaselineTIFFTagSet.COMPRESSION_CCITT_T_4 };
char[] INCH_RESOLUTION_UNIT = new char[] { 2 };
char[] BITS_PER_SAMPLE = new char[] { 1 };
long[][] X_DPI_RESOLUTION = new long[][] { { 200, 1 } };
long[][] Y_DPI_RESOLUTION = new long[][] { { 200, 1 } };
TIFFDirectory ifd = TIFFDirectory.createFromMetadata(imageMetadata);
BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
TIFFTag tagResUnit = base.getTag(BaselineTIFFTagSet.TAG_RESOLUTION_UNIT);
TIFFTag tagXRes = base.getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION);
TIFFTag tagYRes = base.getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION);
TIFFTag tagBitSample = base.getTag(BaselineTIFFTagSet.TAG_BITS_PER_SAMPLE);
TIFFTag tagRowStrips = base.getTag(BaselineTIFFTagSet.TAG_ROWS_PER_STRIP);
TIFFTag tagCompression = base.getTag(BaselineTIFFTagSet.TAG_COMPRESSION);
TIFFField fieldResUnit = new TIFFField(tagResUnit, TIFFTag.TIFF_SHORT, 1, INCH_RESOLUTION_UNIT);
TIFFField fieldXRes = new TIFFField(tagXRes, TIFFTag.TIFF_RATIONAL, 1, X_DPI_RESOLUTION);
TIFFField fieldYRes = new TIFFField(tagYRes, TIFFTag.TIFF_RATIONAL, 1, Y_DPI_RESOLUTION);
TIFFField fieldBitSample = new TIFFField(tagBitSample, TIFFTag.TIFF_SHORT, 1, BITS_PER_SAMPLE);
TIFFField fieldCompression = new TIFFField(tagCompression, TIFFTag.TIFF_SHORT, 1, COMPRESSION);
ifd.addTIFFField(fieldResUnit);
ifd.addTIFFField(fieldXRes);
ifd.addTIFFField(fieldYRes);
ifd.addTIFFField(fieldBitSample);
ifd.addTIFFField(fieldCompression);
return ifd.getAsMetadata();
}
答案 0 :(得分:0)
以下代码不一致:
float pageWidthInch = image[i].getWidth() * 72 / 200;
float pageHeightInch = image[i].getHeight() * 72 / 100;
你的源DPI是200x100吗?保持投入一致;选择100或200.这可能是您的问题的根源,特别是因为您将两者都保存为输出200。两者都应该是相同的值,除非你的来源使用不同的DPI作为高度和宽度。
请注意,此代码在您上面提交的代码中显示两次。
如果需要,请在源图像上使用AsTiffTagViewer(我发现检测TIFF文件非常宝贵的免费应用程序)来确认每页的相应值。
在进一步审核后,您可以这样做:
g2.scale(pageWidthInch, pageHeightInch);
您是否考虑过将此通话留给g2.scale()
?