use of javax.imageio.metadata.IIOMetadata in project tess4j by nguyenq.
the class ImageIOHelper method setDPIViaAPI.
/**
* Set DPI using API.
*
* @param imageMetadata original IIOMetadata
* @param dpiX horizontal resolution
* @param dpiY vertical resolution
* @return modified IIOMetadata
* @throws IIOInvalidTreeException
*/
private static IIOMetadata setDPIViaAPI(IIOMetadata imageMetadata, int dpiX, int dpiY) throws IIOInvalidTreeException {
// Derive the TIFFDirectory from the metadata.
TIFFDirectory dir = TIFFDirectory.createFromMetadata(imageMetadata);
// Get {X,Y}Resolution tags.
BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
TIFFTag tagXRes = base.getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION);
TIFFTag tagYRes = base.getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION);
// Create {X,Y}Resolution fields.
TIFFField fieldXRes = new TIFFField(tagXRes, TIFFTag.TIFF_RATIONAL, 1, new long[][] { { dpiX, 1 } });
TIFFField fieldYRes = new TIFFField(tagYRes, TIFFTag.TIFF_RATIONAL, 1, new long[][] { { dpiY, 1 } });
// Append {X,Y}Resolution fields to directory.
dir.addTIFFField(fieldXRes);
dir.addTIFFField(fieldYRes);
// Convert to metadata object.
IIOMetadata metadata = dir.getAsMetadata();
// Add other metadata.
IIOMetadataNode root = new IIOMetadataNode("javax_imageio_1.0");
IIOMetadataNode horiz = new IIOMetadataNode("HorizontalPixelSize");
horiz.setAttribute("value", Double.toString(25.4f / dpiX));
IIOMetadataNode vert = new IIOMetadataNode("VerticalPixelSize");
vert.setAttribute("value", Double.toString(25.4f / dpiY));
IIOMetadataNode dim = new IIOMetadataNode("Dimension");
dim.appendChild(horiz);
dim.appendChild(vert);
root.appendChild(dim);
metadata.mergeTree("javax_imageio_1.0", root);
return metadata;
}
use of javax.imageio.metadata.IIOMetadata in project tess4j by nguyenq.
the class ImageIOHelper method getImageByteBuffer.
/**
* Gets pixel data of an <code>RenderedImage</code> object.
*
* @param image an <code>RenderedImage</code> object
* @return a byte buffer of pixel data
* @throws IOException
*/
public static ByteBuffer getImageByteBuffer(RenderedImage image) throws IOException {
// Set up the writeParam
TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);
// Get tif writer and set output to file
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(TIFF_FORMAT);
if (!writers.hasNext()) {
throw new RuntimeException(JAI_IMAGE_WRITER_MESSAGE);
}
ImageWriter writer = writers.next();
// Get the stream metadata
IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream);
writer.setOutput(ios);
writer.write(streamMetadata, new IIOImage(image, null, null), tiffWriteParam);
// writer.write(image);
writer.dispose();
// ImageIO.write(image, "tiff", ios); // this can be used in lieu of writer
ios.seek(0);
BufferedImage bi = ImageIO.read(ios);
return convertImageData(bi);
}
use of javax.imageio.metadata.IIOMetadata in project tess4j by nguyenq.
the class ImageIOHelper method mergeTiff.
/**
* Merges multiple images into one multi-page TIFF image.
*
* @param imageList a list of <code>IIOImage</code> objects
* @param outputTiff the output TIFF file
* @param compressionType valid values: LZW, CCITT T.6, PackBits
* @throws IOException
*/
public static void mergeTiff(List<IIOImage> imageList, File outputTiff, String compressionType) throws IOException {
if (imageList == null || imageList.isEmpty()) {
// if no image
return;
}
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(TIFF_FORMAT);
if (!writers.hasNext()) {
throw new RuntimeException(JAI_IMAGE_WRITER_MESSAGE);
}
ImageWriter writer = writers.next();
// Set up the writeParam
TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
// tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED); // comment out to preserve original sizes
if (compressionType != null) {
tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
tiffWriteParam.setCompressionType(compressionType);
}
// Get the stream metadata
IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam);
ImageOutputStream ios = ImageIO.createImageOutputStream(outputTiff);
writer.setOutput(ios);
int dpiX = 300;
int dpiY = 300;
for (IIOImage iioImage : imageList) {
// Get the default image metadata.
ImageTypeSpecifier imageType = ImageTypeSpecifier.createFromRenderedImage(iioImage.getRenderedImage());
IIOMetadata imageMetadata = writer.getDefaultImageMetadata(imageType, null);
imageMetadata = setDPIViaAPI(imageMetadata, dpiX, dpiY);
iioImage.setMetadata(imageMetadata);
}
IIOImage firstIioImage = imageList.remove(0);
writer.write(streamMetadata, firstIioImage, tiffWriteParam);
int i = 1;
for (IIOImage iioImage : imageList) {
writer.writeInsert(i++, iioImage, tiffWriteParam);
}
ios.close();
writer.dispose();
}
use of javax.imageio.metadata.IIOMetadata in project CodenameOne by codenameone.
the class AnimationImpl method getPixelOffsets.
public static Point getPixelOffsets(ImageReader reader, int num) throws IOException {
IIOMetadata meta = reader.getImageMetadata(num);
Point point = new Point(-1, -1);
Node root = meta.getAsTree("javax_imageio_1.0");
// printNodeTree(root);
for (Node c = root.getFirstChild(); c != null; c = c.getNextSibling()) {
String name = c.getNodeName();
if ("Dimension".equals(name)) {
for (c = c.getFirstChild(); c != null; c = c.getNextSibling()) {
name = c.getNodeName();
if ("HorizontalPixelOffset".equals(name))
point.x = getValueAttribute(c);
else if ("VerticalPixelOffset".equals(name))
point.y = getValueAttribute(c);
}
return point;
}
}
return point;
}
use of javax.imageio.metadata.IIOMetadata in project CodenameOne by codenameone.
the class TimelineEditor method getDisposalMethod.
static String getDisposalMethod(ImageReader reader, int num) throws IOException {
IIOMetadata meta = reader.getImageMetadata(num);
Node parent = meta.getAsTree("javax_imageio_gif_image_1.0");
// printNodeTree(parent);
NodeList root = parent.getChildNodes();
for (int iter = 0; iter < root.getLength(); iter++) {
Node n = root.item(iter);
if (n.getNodeName().equals("GraphicControlExtension")) {
return n.getAttributes().getNamedItem("disposalMethod").getNodeValue();
}
}
return null;
}
Aggregations