通过下列代码可以使文件进一步缩小,因为生成后的空间RGB色彩用5bit来存放,问题是还能进一步缩小吗??
BufferedImage bi3 = new BufferedImage(176,220, BufferedImage.TYPE_USHORT_555_RGB);
Graphics2D g2D = bi3.createGraphics();
g2D.drawRenderedImage(bi2, new AffineTransform(ratio1,ratio2));
ImageIO.write(bi3,"PNG",new File("ddd.png");我想到了自己定义色彩模式,RGB分别用4bit来存放:
ColorModel colorModel = new DirectColorModel(12,
                                                  0xf00,
                                                  0x0f0,
                                                  0x00f
                                                  );
        WritableRaster       raster = colorModel.createCompatibleWritableRaster(176,220);
        BufferedImage bi3 = new BufferedImage(colorModel,raster,false,null);
        Graphics2D g2D = bi3.createGraphics();
        g2D.drawRenderedImage(bi2, new AffineTransform());
        ImageIO.write(bi3,"PNG",new File("1020.png"));
但是程序出错,有哪位高手可以指出其中的错误??不胜感激

解决方案 »

  1.   

    我感觉直接用
    imageIcon.getImage().getScaledInstance( -1, getHeight(), Image.SCALE_DEFAULT );
    不是效果不错吗?
    那样比色好象不会起太大的变化.用这个再存盘是不是图片的大小不变?如果通过修改比色来压缩,我想用java怕是不好处理.
      

  2.   

    我试过了,用BufferedImage.TYPE_USHORT_555_RGB,真彩色的JPG图片生成后35.8K,用xyz4008你的方法生成后54.6K,其实BufferedImage就是通过自己定义ColorModel来实现转化的,现在我自己定义的这段代码报Coordinate out of bounds错误?是什么原因
      

  3.   

    package com.hotmail.walksing.module.graphics;
    import java.awt.Image;
    import java.awt.Graphics2D;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.FileOutputStream;
    import java.io.PrintWriter;
    import javax.swing.ImageIcon;
    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;import javax.servlet.*;
    import javax.servlet.http.*;//docs : http://java.sun.com/developer/TechTips/1999/tt1021.html
    //java Thumbnail <original.{gif,jpg}>  <thumbnail.jpg> <maxDim>public class ThumbCreater {

    public static double SRC_Height = 0;
    public static double SRC_Width = 0;
    public static int scaledW = 0;
    public static int scaledH = 0;
    public static PrintWriter out;
    public static HttpServletResponse response;
    public static HttpServletRequest request;
    public static String mimeType = "application/octet-stream";
    public static String werror;
    public static BufferedImage outImage;
    public static void create(String[] args) {
    createThumbnail(args[0], args[1], Integer.parseInt(args[2]));
    }

    /**
    * Reads an image in a file and creates 
    * a thumbnail in another file.
    * @param orig The name of image file.
    * @param thumb The name of thumbnail file.  
    * Will be created if necessary.
    * @param maxDim The width and height of 
    * the thumbnail must 
    * be maxDim pixels or less.
    */
    public static void createThumbnail(
    String orig, String thumb, int maxDim) {
    try {
        // Get the image from a file.
        Image inImage = new ImageIcon(
                      orig).getImage();

    SRC_Height = (double) inImage.getHeight(null);
    SRC_Width = (double) inImage.getWidth(null);
    //System.out.println(SRC_Height + "X" + SRC_Width);
        // Determine the scale.
        double scale = (double)maxDim/(
           double)inImage.getHeight(null);
        if (inImage.getWidth(
         null) > inImage.getHeight(null)) {
            scale = (double)maxDim/(
            double)inImage.getWidth(null);
        }

        // Determine size of new image. 
        //One of them
        // should equal maxDim.
        scaledW = (int)(
         scale*inImage.getWidth(null));
        scaledH = (int)(
         scale*inImage.getHeight(null));

        // Create an image buffer in 
        //which to paint on.
        outImage = 
          new BufferedImage(scaledW, scaledH,
            BufferedImage.TYPE_INT_RGB);

        // Set the scale.
        AffineTransform tx = 
          new AffineTransform();

        // If the image is smaller than 
        //the desired image size,
        // don't bother scaling.
        if (scale < 1.0d) {
            tx.scale(scale, scale);
        }

        // Paint image.
        Graphics2D g2d = 
         outImage.createGraphics();
        g2d.drawImage(inImage, tx, null);
        g2d.dispose();

        // JPEG-encode the image 
        //and write to file.
        OutputStream os = 
         new FileOutputStream(thumb);
        JPEGImageEncoder encoder = 
          JPEGCodec.createJPEGEncoder(os);
        encoder.encode(outImage);
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    } public static void outPut(HttpServletResponse response){
    // Create an image buffer in 
    //which to paint on.
    response.setContentType(mimeType);

    OutputStream os = null;
    JPEGImageEncoder encoder = null;
    try{
    os= response.getOutputStream();
    encoder = JPEGCodec.createJPEGEncoder(os);
    encoder.encode(outImage);
    os.close();
    }catch(java.io.IOException ioe){
    werror = ioe.toString();
    System.err.println(werror);
    }
    }
        
    }
      
      

  4.   

    我的要求是要生成最小尺寸的PNG格式图片阿啊,WalkSing的方法我试过,应该和我原来的没有多大区别,我对java图形操作也不是很熟悉,不知道是不是还有我没有发现的地方,WalkSing或者哪位高手有中文的文档吗??
      

  5.   

    double scale1 = (double)176/(double)inImage.getWidth(null);
            double scale2 = (double)220/(double)inImage.getHeight(null);
            BufferedImage outImage =     new BufferedImage(176,220, BufferedImage.TYPE_USHORT_555_RGB);
            AffineTransform tx =   new AffineTransform();
            tx.scale(scale1, scale2);
        Graphics2D g2d =    outImage.createGraphics();
    //g2d.drawImage(inImage, tx, null);
            g2d.drawRenderedImage(inImage,tx);
        // JPEG-encode the image
        //and write to file.
            try{
        OutputStream os =   new FileOutputStream("4410.png");
       ImageEncoder encoder = new PNGImageEncoder(os,null);
                ColorModel colorModel = new DirectColorModel(8, 0xc0,0x38, 0x03);
               // ColorModel colorModel = new DirectColorModel(14,0x3e00 ,0x01E0,0x001f);            WritableRaster       raster = colorModel.createCompatibleWritableRaster(176,220);
        encoder.encode(outImage.getRaster(),colorModel);
        os.close();
    以上的代码问题出在哪里,我生成后的PNG图片非常暗,几乎是看不清楚,而且有的尺寸还增加了(相对于555颜色)