首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

图形读取有关问题

2012-09-17 
图形读取问题通过文件流的形式读取图片时如何设定他的像素ImageInputStream iis null InputStream is

图形读取问题
通过文件流的形式读取图片时如何设定他的像素
ImageInputStream iis = null ; 
InputStream is = new ByteArrayInputStream(bytes);
iis = ImageIO.createImageInputStream(is); 
如果这张图片原始像素为1024x768;那我想给一个240X180的新像素给他
请问该如何操作。

[解决办法]
将iis转换为BufferedImage对象然后进行如下操作

Java code
/**     * 改变图片大小     *      * @param img     * @param weight     * @param height     * @return     */    public BufferedImage modifySize(BufferedImage img, int width, int height) {        try {            int w = img.getWidth();            int h = img.getHeight();            double wRation = (new Integer(width)).doubleValue() / w;            double hRation = (new Integer(height)).doubleValue() / h;            Image image = img.getScaledInstance(width, height,                    Image.SCALE_SMOOTH);            AffineTransformOp op = new AffineTransformOp(AffineTransform                    .getScaleInstance(wRation, hRation), null);            image = op.filter(img, null);            img = (BufferedImage) image;        } catch (Exception e) {            e.printStackTrace();        }        return img;    } 

热点排行