use of javax.imageio.IIOImage in project alliance by codice.
the class NitfPreStoragePlugin method renderToJpeg2k.
private byte[] renderToJpeg2k(final BufferedImage bufferedImage) throws IOException {
BufferedImage imageToCompress = bufferedImage;
if (bufferedImage.getColorModel().getNumComponents() == ARGB_COMPONENT_COUNT) {
imageToCompress = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = imageToCompress.createGraphics();
g.drawImage(bufferedImage, 0, 0, null);
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
J2KImageWriter writer = new J2KImageWriter(new J2KImageWriterSpi());
J2KImageWriteParam writeParams = (J2KImageWriteParam) writer.getDefaultWriteParam();
writeParams.setLossless(false);
writeParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParams.setCompressionType("JPEG2000");
writeParams.setCompressionQuality(0.0f);
ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
writer.setOutput(ios);
writer.write(null, new IIOImage(imageToCompress, null, null), writeParams);
writer.dispose();
ios.close();
return os.toByteArray();
}
use of javax.imageio.IIOImage in project acs-aem-commons by Adobe-Consulting-Services.
the class ProgressiveJpeg method write.
/**
* For JPEG images, this method behaves similar to {@link Layer#write(String, double, OutputStream)}. The major
* difference is that it uses progressive encoding.
*
* @param layer the layer with the image to write to the output stream
* @param quality JPEG compression quality between 0 and 1
* @param out target output stream
* @throws IOException if anything goes wrong
*/
public static void write(Layer layer, double quality, OutputStream out) throws IOException {
ImageWriter writer = null;
ImageOutputStream imageOut = null;
try {
ImageWriteParam iwp = new JPEGImageWriteParam(null);
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setProgressiveMode(ImageWriteParam.MODE_DEFAULT);
iwp.setCompressionQuality((float) quality);
writer = ImageIO.getImageWritersBySuffix("jpeg").next();
imageOut = ImageIO.createImageOutputStream(out);
writer.setOutput(imageOut);
BufferedImage image = getRgbImage(layer);
writer.write(null, new IIOImage(image, null, null), iwp);
} finally {
if (writer != null) {
writer.dispose();
}
if (imageOut != null) {
try {
imageOut.close();
} catch (IOException e) {
// ignore
}
}
}
}
use of javax.imageio.IIOImage in project tess4j by nguyenq.
the class ImageIOHelper method getIIOImageList.
/**
* Gets a list of <code>IIOImage</code> objects for an image file.
*
* @param imageFile input image file. It can be any of the supported
* formats, including TIFF, JPEG, GIF, PNG, BMP, JPEG, and PDF if GPL
* Ghostscript is installed
* @return a list of <code>IIOImage</code> objects
* @throws IOException
*/
public static List<IIOImage> getIIOImageList(File imageFile) throws IOException {
File workingTiffFile = null;
ImageReader reader = null;
ImageInputStream iis = null;
try {
// convert PDF to TIFF
if (imageFile.getName().toLowerCase().endsWith(".pdf")) {
workingTiffFile = PdfUtilities.convertPdf2Tiff(imageFile);
imageFile = workingTiffFile;
}
List<IIOImage> iioImageList = new ArrayList<IIOImage>();
String imageFileName = imageFile.getName();
String imageFormat = imageFileName.substring(imageFileName.lastIndexOf('.') + 1);
if (imageFormat.matches("(pbm|pgm|ppm)")) {
imageFormat = "pnm";
} else if (imageFormat.matches("(jp2|j2k|jpf|jpx|jpm)")) {
imageFormat = "jpeg2000";
}
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(imageFormat);
if (!readers.hasNext()) {
throw new RuntimeException(JAI_IMAGE_READER_MESSAGE);
}
reader = readers.next();
iis = ImageIO.createImageInputStream(imageFile);
reader.setInput(iis);
int imageTotal = reader.getNumImages(true);
for (int i = 0; i < imageTotal; i++) {
// IIOImage oimage = new IIOImage(reader.read(i), null, reader.getImageMetadata(i));
IIOImage oimage = reader.readAll(i, reader.getDefaultReadParam());
iioImageList.add(oimage);
}
return iioImageList;
} finally {
try {
if (iis != null) {
iis.close();
}
if (reader != null) {
reader.dispose();
}
} catch (Exception e) {
// ignore
}
if (workingTiffFile != null && workingTiffFile.exists()) {
workingTiffFile.delete();
}
}
}
use of javax.imageio.IIOImage 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 javax.imageio.IIOImage 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();
}
Aggregations