尝试将文本添加到多页(多图像)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();
}