天天看點

bufferedimage 轉換成 inputstream

<span style="font-size:18px;">BufferedImage imgBar = 。。。。。。;
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(imgBar, "jpg", imOut);
InputStream is = new ByteArrayInputStream(bs.toByteArray());</span>
           

使用ImageIo時,發現圖檔失貞。

BufferedImage img = chart.createBufferedImage(600, 300,5, null);

createBufferedImage時加上一個參數5即可。

/**
     * Represents an image with 8-bit RGB color components, corresponding
     * to a Windows-style BGR color model) with the colors Blue, Green,
     * and Red stored in 3 bytes.  There is no alpha.  The image has a
     * <code>ComponentColorModel</code>.
     * When data with non-opaque alpha is stored
     * in an image of this type,
     * the color data must be adjusted to a non-premultiplied form
     * and the alpha discarded,
     * as described in the
     * {@link java.awt.AlphaComposite} documentation.
     */
    public static final int TYPE_3BYTE_BGR = 5;
           

參考:

http://stackoverflow.com/questions/4386446/problem-using-imageio-write-jpg-file