List of usage examples for javax.imageio ImageWriteParam setSourceBands
public void setSourceBands(int[] sourceBands)
From source file:org.apache.flex.compiler.internal.embedding.transcoders.JPEGTranscoder.java
private byte[] bufferedImageToJPEG(ImageInfo imageInfo, int[] pixels) throws Exception { BufferedImage bufferedImage = new BufferedImage(imageInfo.width, imageInfo.height, BufferedImage.TYPE_INT_ARGB); bufferedImage.setRGB(0, 0, imageInfo.width, imageInfo.height, pixels, 0, imageInfo.width); ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next(); ImageWriteParam writeParam = writer.getDefaultWriteParam(); ColorModel colorModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff); ImageTypeSpecifier imageTypeSpecifier = new ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(1, 1)); writeParam.setDestinationType(imageTypeSpecifier); writeParam.setSourceBands(new int[] { 0, 1, 2 }); writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); float q = 1.0f; if (quality != null) q = quality.floatValue();//from ww w.j ava 2s . com writeParam.setCompressionQuality(q); DAByteArrayOutputStream buffer = new DAByteArrayOutputStream(); writer.setOutput(new MemoryCacheImageOutputStream(buffer)); IIOImage ioImage = new IIOImage(bufferedImage, null, null); writer.write(null, ioImage, writeParam); writer.dispose(); return buffer.getDirectByteArray(); }
From source file:org.polymap.core.data.image.ImageEncodeProcessor.java
private void imageioEncodeJPEG(Image image, ChunkedResponseOutputStream out) throws IOException { // this code is from http://forums.sun.com/thread.jspa?threadID=5197061 ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next(); ImageWriteParam param = writer.getDefaultWriteParam(); param.setSourceBands(new int[] { 0, 1, 2 }); ColorModel cm = new DirectColorModel(24, 0x00ff0000, // Red 0x0000ff00, // Green 0x000000ff, // Blue 0x0); // Alpha param.setDestinationType(new ImageTypeSpecifier(cm, cm.createCompatibleSampleModel(1, 1))); ImageOutputStream imageOut = ImageIO.createImageOutputStream(out); writer.setOutput(imageOut);/*w w w. j a v a 2 s . c o m*/ writer.write(null, new IIOImage((RenderedImage) image, null, null), param); writer.dispose(); imageOut.close(); }