use of javax.imageio.ImageTypeSpecifier in project protools by SeanDragon.
the class ToolImageResize method resize.
/**
* 缩放gif图片
*
* @param originalFile
* 原图片
* @param resizedFile
* 缩放后的图片
* @param newWidth
* 宽度
* @param quality
* 缩放比例 (等比例)
*
* @throws IOException
*/
private static void resize(File originalFile, File resizedFile, int newWidth, float quality) throws IOException {
if (quality < 0 || quality > 1) {
throw new IllegalArgumentException("Quality has to be between 0 and 1");
}
ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
Image i = ii.getImage();
Image resizedImage = null;
int iWidth = i.getWidth(null);
int iHeight = i.getHeight(null);
if (iWidth > iHeight) {
resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight) / iWidth, Image.SCALE_SMOOTH);
} else {
resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight, newWidth, Image.SCALE_SMOOTH);
}
// This code ensures that all the pixels in the image are loaded.
Image temp = new ImageIcon(resizedImage).getImage();
// Create the buffered image.
BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
// Copy image to buffered image.
Graphics g = bufferedImage.createGraphics();
// Clear background and paint the image.
g.setColor(Color.white);
g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
g.drawImage(temp, 0, 0, null);
g.dispose();
// Soften.
float softenFactor = 0.05f;
float[] softenArray = { 0, softenFactor, 0, softenFactor, 1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
Kernel kernel = new Kernel(3, 3, softenArray);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
bufferedImage = cOp.filter(bufferedImage, null);
// Write the jpeg to a file.
try (FileOutputStream out = new FileOutputStream(resizedFile)) {
ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("jpg").next();
ImageOutputStream ios = ImageIO.createImageOutputStream(out);
imageWriter.setOutput(ios);
// and metadata
IIOMetadata imageMetaData = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(bufferedImage), null);
JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam();
jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
jpegParams.setCompressionQuality(quality);
imageWriter.write(imageMetaData, new IIOImage(bufferedImage, null, null), jpegParams);
}
}
use of javax.imageio.ImageTypeSpecifier in project digilib by robcast.
the class ImageLoaderDocuImage method loadSubimage.
/*
* Load an image file into the Object.
*
* @see digilib.image.DocuImageImpl#loadSubimage(digilib.io.ImageInput, java.awt.Rectangle, int)
*/
public void loadSubimage(ImageInput ii, Rectangle region, int prescale) throws FileOpException {
logger.debug("loadSubimage");
this.input = ii;
// ImageReader reader = null;
try {
reader = getReader(ii);
/*
* set up reader parameters
*/
ImageReadParam readParam = reader.getDefaultReadParam();
readParam.setSourceRegion(region);
if (prescale > 1) {
readParam.setSourceSubsampling(prescale, prescale, 0, 0);
}
if (imageHacks.get(Hacks.setDestSrgb)) {
/*
* try to set target color space to sRGB
*/
for (Iterator<ImageTypeSpecifier> i = reader.getImageTypes(0); i.hasNext(); ) {
ImageTypeSpecifier type = (ImageTypeSpecifier) i.next();
ColorModel cm = type.getColorModel();
ColorSpace cs = cm.getColorSpace();
logger.debug("loadSubimage: possible color model:" + cm + " color space:" + cs);
if (cs.getNumComponents() < 3 && !imageHacks.get(Hacks.setDestSrgbForNonRgb)) {
// if the first type is not RGB do nothing
logger.debug("loadSubimage: image is not RGB " + type);
break;
}
if (cs.isCS_sRGB()) {
logger.debug("loadSubimage: substituted sRGB destination type " + type);
readParam.setDestinationType(type);
break;
}
}
}
/*
* read image
*/
logger.debug("loadSubimage: loading..");
img = reader.read(0, readParam);
logger.debug("loadSubimage: loaded " + img);
// invalidate image size if it was set
imageSize = null;
/*
* downconvert highcolor images
*/
if (img.getColorModel().getComponentSize(0) > 8) {
logger.debug("loadSubimage: converting to 8bit");
int type = BufferedImage.TYPE_INT_RGB;
if (img.getColorModel().hasAlpha()) {
type = BufferedImage.TYPE_INT_ARGB;
}
BufferedImage lcImg = new BufferedImage(img.getWidth(), img.getHeight(), type);
lcImg.createGraphics().drawImage(img, null, 0, 0);
img = lcImg;
}
} catch (IOException e) {
throw new FileOpException("Unable to load File!", e);
} finally {
if (!reuseReader && reader != null) {
reader.dispose();
}
}
}
use of javax.imageio.ImageTypeSpecifier in project openolat by klemens.
the class ImageHelperImpl method calcScaledSize.
private static SizeAndBufferedImage calcScaledSize(ImageInputStream stream, String suffix, int maxWidth, int maxHeight, boolean fill) {
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
if (iter.hasNext()) {
ImageReader reader = iter.next();
try {
reader.setInput(stream, true, true);
int width = reader.getWidth(reader.getMinIndex());
int height = reader.getHeight(reader.getMinIndex());
Size size = new Size(width, height, false);
Size scaledSize = computeScaledSize(width, height, maxWidth, maxHeight, fill);
SizeAndBufferedImage all = new SizeAndBufferedImage(size, scaledSize);
int readerMinIndex = reader.getMinIndex();
ImageReadParam param = reader.getDefaultReadParam();
Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
while (imageTypes.hasNext()) {
try {
ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
int bufferedImageType = imageTypeSpecifier.getBufferedImageType();
if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) {
param.setDestinationType(imageTypeSpecifier);
}
double memoryKB = (width * height * 4) / 1024d;
if (memoryKB > 2000) {
// check limit at 20MB
double free = Runtime.getRuntime().freeMemory() / 1024d;
if (free > memoryKB) {
all.setImage(reader.read(readerMinIndex, param));
} else {
// make sub sampling to save memory
int ratio = (int) Math.round(Math.sqrt(memoryKB / free));
param.setSourceSubsampling(ratio, ratio, 0, 0);
all.setImage(reader.read(readerMinIndex, param));
}
} else {
all.setImage(reader.read(readerMinIndex, param));
}
return all;
} catch (IllegalArgumentException e) {
log.warn(e.getMessage(), e);
}
}
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
reader.dispose();
}
} else {
log.error("No reader found for given format: " + suffix, null);
}
return null;
}
Aggregations