title image


Smiley Lösung


static InputStream scaleImage(final BufferedImage img, final int maxWidth,

final int maxHeight, final float quality, final boolean progressive)

throws IOException {

final double maxRatio = maxWidth / maxHeight;

final double srcHeight = img.getHeight();

final double srcWidth = img.getWidth();

final double srcRatio = srcWidth / srcHeight;

final double scaleFactor;

if (srcRatio < maxRatio) {

scaleFactor = maxHeight / srcHeight;

} else {

scaleFactor = maxWidth / srcWidth;

}

final int dstHeight = (int) (srcHeight * scaleFactor);

final int dstWidth = (int) (srcWidth * scaleFactor);

final BufferedImage dest = new BufferedImage(dstWidth, dstHeight,

BufferedImage.TYPE_INT_RGB);

dest.createGraphics().drawRenderedImage(img,

AffineTransform.getScaleInstance(scaleFactor, scaleFactor));

final ByteArrayOutputStream outStream = new ByteArrayOutputStream(

80 * 1024);

// ImageIO.write(dest, "JPG", outStream);

Iterator iter = ImageIO.getImageWritersByFormatName("jpg");

if (!iter.hasNext()) {

// TODO

}

final ImageWriter writer = iter.next();

final ImageOutputStream ios = ImageIO

.createImageOutputStream(outStream);

writer.setOutput(ios);

final ImageWriteParam iwparam = writer.getDefaultWriteParam();

iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);

iwparam.setCompressionQuality(quality);

if (progressive && iwparam.canWriteProgressive()) {

iwparam.setProgressiveMode(ImageWriteParam.MODE_DEFAULT);

}

writer.write(null, new IIOImage(dest, null, null), iwparam);

ios.flush();

writer.dispose();

ios.close();

return new ByteArrayInputStream(outStream.toByteArray());

}





geschrieben von

Login

E-Mail:
  

Passwort:
  

Beitrag anfügen

Symbol:
 
 
 
 
 
 
 
 
 
 
 
 
 

Überschrift: