複数ページ (複数の画像) の 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;
Save メソッドのコード:
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();
}