Cita:
este es el codigo que estoy utilizandoBuenas... estoy adicionando un texto a una imagen multi tiff, y tengo el siguiente problema.
El texto es adicionado sin problemas, pero la imagen resultante no es igual a la imagen original, ya que el contenido de la imagen resultante es mas grande que la original
no tengo idea de como lograr que el contenido quede igual al original
si tienen alguna idea gracias
El texto es adicionado sin problemas, pero la imagen resultante no es igual a la imagen original, ya que el contenido de la imagen resultante es mas grande que la original
no tengo idea de como lograr que el contenido quede igual al original
si tienen alguna idea gracias
public void texto() throws IOException{
File file = new File("C:/Pruebas Cadena/tifSources/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);
// convertRenderedImage(op1);//
BufferedImage pg1 = (new BufferedImage(op1.getWidth(), op1.getHeight(),BufferedImage.TYPE_INT_RGB));
image[i] = pg1;
Graphics2D g2 = image[i].createGraphics();
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.dispose();
}
save(image,"C:/Prueb-14.tif");
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public BufferedImage convertRenderedImage(RenderedImage img) {
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, isAlphaPremultiplied, properties);
img.copyData(raster);
return result;
}
private static void save(BufferedImage[] b, String tif) throws IOException {
/*
* Get a TIFF writer and set its output.
*/
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_EXPL ICIT);
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);
}
/*
* Dispose & Close file
*/
writer.endWriteSequence();
writer.dispose();
writer = null;
fios.close();
}
private static IIOMetadata createImageMetadata(IIOMetadata imageMetadata)
throws IIOInvalidTreeException {
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 } };
/*
* Get the IFD (Image File Directory) which is the root of all the tags
* for this image. From here we can get all the tags in the image.
*/
TIFFDirectory ifd = TIFFDirectory.createFromMetadata(imageMetadata);
/*
* Create the necessary TIFF tags that we want to add to the image
* metadata
*/
BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
/*
* Resolution tags
*/
TIFFTag tagResUnit = base
.getTag(BaselineTIFFTagSet.TAG_RESOLUTION_UNIT);
TIFFTag tagXRes = base.getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION);
TIFFTag tagYRes = base.getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION);
/*
* BitsPerSample tag
*/
TIFFTag tagBitSample = base
.getTag(BaselineTIFFTagSet.TAG_BITS_PER_SAMPLE);
/*
* Row and Strip tags
*/
TIFFTag tagRowStrips = base
.getTag(BaselineTIFFTagSet.TAG_ROWS_PER_STRIP);
/*
* Compression tag
*/
TIFFTag tagCompression = base
.getTag(BaselineTIFFTagSet.TAG_COMPRESSION);
/*
* Set the tag values
*/
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 fieldRowStrips = new TIFFField(tagRowStrips,
// TIFFTag.TIFF_LONG, 1, new long[] { IMAGE_HEIGHT });
TIFFField fieldCompression = new TIFFField(tagCompression,
TIFFTag.TIFF_SHORT, 1, COMPRESSION);
/*
* Add the new tag/value sets to the image metadata
*/
ifd.addTIFFField(fieldResUnit);
ifd.addTIFFField(fieldXRes);
ifd.addTIFFField(fieldYRes);
ifd.addTIFFField(fieldBitSample);
// ifd.addTIFFField(fieldRowStrips);
ifd.addTIFFField(fieldCompression);
return ifd.getAsMetadata();
}
gracias por la colaboracion