org.jpedal.examples.handlers.StandardImageIO Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of OpenViewerFX Show documentation
Show all versions of OpenViewerFX Show documentation
Open Source (LGPL) JavaFX PDF Viewer for NetBeans plugin
/*
* ===========================================
* Java Pdf Extraction Decoding Access Library
* ===========================================
*
* Project Info: http://www.idrsolutions.com
* Help section for developers at http://www.idrsolutions.com/support/
*
* (C) Copyright 1997-2017 IDRsolutions and Contributors.
*
* This file is part of JPedal/JPDF2HTML5
*
@LICENSE@
*
* ---------------
* StandardImageIO.java
* ---------------
*/
package org.jpedal.examples.handlers;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import org.jpedal.color.GenericColorSpace;
import org.jpedal.exception.PdfException;
import org.jpedal.external.ExternalHandlers;
import org.jpedal.external.ImageHelper;
import org.jpedal.utils.LogWriter;
public class StandardImageIO implements ImageHelper {
public StandardImageIO() {
ImageIO.setUseCache(false);
}
/**
* @param image
* @param type
* @param file_name
* @throws IOException
*/
@Override
public void write(final BufferedImage image, final String type, final String file_name) throws IOException {
ExternalHandlers.ImageLib.write(image, type, file_name, GenericColorSpace.fasterPNG);
}
/**
* @param file_name (including path)
* @return
*/
@Override
public BufferedImage read(final String file_name) {
BufferedImage image = null;
try {
image = ImageIO.read(new File(file_name));
//BufferedInputStream in = new BufferedInputStream(new FileInputStream(file_name));
//image = ImageIO.read(in);
//in.close();
} catch (final IOException e) {
LogWriter.writeLog("Exception: " + e.getMessage());
} catch (final Error err) {
LogWriter.writeLog("Error: " + err.getMessage());
throw new RuntimeException("Error " + err + " loading " + file_name + " with ImageIO");
}
return image;
}
/**
* @param data (final byte[] data for image file - PNG, TIF, JPEG)
* @return
* @throws IOException
*/
@Override
public synchronized BufferedImage read(final byte[] data) throws IOException {
final ByteArrayInputStream bis = new ByteArrayInputStream(data);
ImageIO.setUseCache(false);
return ImageIO.read(bis);
}
/**
* @param data (binary data for image file - PNG, TIF, JPEG)
* @return
* @throws IOException
*/
@Override
public Raster readRasterFromJPeg(final byte[] data) throws IOException {
Raster ras = null;
ImageReader iir = null;
ImageInputStream iin = null;
final ByteArrayInputStream in = new ByteArrayInputStream(data);
//suggestion from Carol
try {
final Iterator iterator = ImageIO.getImageReadersByFormatName("JPEG");
while (iterator.hasNext()) {
final ImageReader o = iterator.next();
iir = o;
if (iir.canReadRaster()) {
break;
}
}
ImageIO.setUseCache(false);
iin = ImageIO.createImageInputStream(in);
iir.setInput(iin, true); //new MemoryCacheImageInputStream(in));
ras = iir.readRaster(0, null);
} catch (final Exception e) {
LogWriter.writeLog("Unable to find jars on classpath " + e);
} finally {
if (in != null) {
in.close();
}
if (iin != null) {
iin.flush();
iin.close();
}
if (iir != null) {
iir.dispose();
}
}
return ras;
}
/**
* @param data (binary data for image file - PNG, TIF, JPEG)
* @param w image width in pixels
* @param h image height in pixels
* @param pX sensible image width in pixels (ie what we should downscale to)
* @param pY sensible image height in pixels (ie what we should downscale
* to)
* @return
* @throws PdfException
*/
@Override
public BufferedImage JPEG2000ToRGBImage(final byte[] data, final int w, final int h, final int pX, final int pY) throws PdfException {
return null;
}
}