use of com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam in project tess4j by nguyenq.
the class ImageIOHelper method createTiffFiles.
public static List<File> createTiffFiles(List<IIOImage> imageList, int index, int dpiX, int dpiY) throws IOException {
List<File> tiffFiles = new ArrayList<File>();
// 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);
// all if index == -1; otherwise, only index-th
for (IIOImage oimage : (index == -1 ? imageList : imageList.subList(index, index + 1))) {
if (dpiX != 0 && dpiY != 0) {
// Get the default image metadata.
ImageTypeSpecifier imageType = ImageTypeSpecifier.createFromRenderedImage(oimage.getRenderedImage());
IIOMetadata imageMetadata = writer.getDefaultImageMetadata(imageType, null);
imageMetadata = setDPIViaAPI(imageMetadata, dpiX, dpiY);
oimage.setMetadata(imageMetadata);
}
File tiffFile = File.createTempFile(OUTPUT_FILE_NAME, TIFF_EXT);
ImageOutputStream ios = ImageIO.createImageOutputStream(tiffFile);
writer.setOutput(ios);
writer.write(streamMetadata, oimage, tiffWriteParam);
ios.close();
tiffFiles.add(tiffFile);
}
writer.dispose();
return tiffFiles;
}
use of com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam in project tess4j by nguyenq.
the class ImageIOHelper method mergeTiff.
/**
* Merges multiple images into one multi-page TIFF image.
*
* @param inputImages an array of image files
* @param outputTiff the output multi-page TIFF file
* @throws IOException
*/
public static void mergeTiff(File[] inputImages, File outputTiff) throws IOException {
if (inputImages.length == 0) {
// 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); // commented out to preserve original sizes
// Get the stream metadata
IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam);
ImageOutputStream ios = ImageIO.createImageOutputStream(outputTiff);
writer.setOutput(ios);
boolean firstPage = true;
int index = 1;
for (File inputImage : inputImages) {
List<IIOImage> iioImages = getIIOImageList(inputImage);
for (IIOImage iioImage : iioImages) {
if (firstPage) {
writer.write(streamMetadata, iioImage, tiffWriteParam);
firstPage = false;
} else {
writer.writeInsert(index++, iioImage, tiffWriteParam);
}
}
}
ios.close();
writer.dispose();
}
use of com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam in project tess4j by nguyenq.
the class ImageIOHelper method createTiffFiles.
/**
* Creates a list of TIFF image files from an image file. It basically
* converts images of other formats to TIFF format, or a multi-page TIFF
* image to multiple TIFF image files.
*
* @param imageFile input image file
* @param index an index of the page; -1 means all pages, as in a multi-page
* TIFF image
* @param preserve preserve compression mode
* @return a list of TIFF image files
* @throws IOException
*/
public static List<File> createTiffFiles(File imageFile, int index, boolean preserve) throws IOException {
List<File> tiffFiles = new ArrayList<File>();
String imageFileName = imageFile.getName();
String imageFormat = imageFileName.substring(imageFileName.lastIndexOf('.') + 1);
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(imageFormat);
if (!readers.hasNext()) {
throw new RuntimeException(JAI_IMAGE_READER_MESSAGE);
}
ImageReader reader = readers.next();
ImageInputStream iis = ImageIO.createImageInputStream(imageFile);
reader.setInput(iis);
// Read the stream metadata
// IIOMetadata streamMetadata = reader.getStreamMetadata();
// Set up the writeParam
TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
if (!preserve) {
// not preserve original sizes; decompress
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();
// Read the stream metadata
IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam);
int imageTotal = reader.getNumImages(true);
for (int i = 0; i < imageTotal; i++) {
// all if index == -1; otherwise, only index-th
if (index == -1 || i == index) {
// BufferedImage bi = reader.read(i);
// IIOImage oimage = new IIOImage(bi, null, reader.getImageMetadata(i));
IIOImage oimage = reader.readAll(i, reader.getDefaultReadParam());
File tiffFile = File.createTempFile(OUTPUT_FILE_NAME, TIFF_EXT);
ImageOutputStream ios = ImageIO.createImageOutputStream(tiffFile);
writer.setOutput(ios);
writer.write(streamMetadata, oimage, tiffWriteParam);
ios.close();
tiffFiles.add(tiffFile);
}
}
writer.dispose();
reader.dispose();
return tiffFiles;
}
use of com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam 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 com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam 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();
}
Aggregations