日期:2014-05-19  浏览次数:20546 次

图形读取问题
通过文件流的形式读取图片时如何设定他的像素
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;
    }