BufferedImage bin=ImageIO.read(new File("g:/1.jpg"));
    
     ImageIO.write(bin,"jpg",new File("g:/2.jpg"));
为什么图片会变小呢?怎么样能保持图片大小不变

解决方案 »

  1.   

    BufferedImage src = ImageIO.read(url);
    int width = src.getWidth(null);
    int height = src.getHeight(null);
    //这里BufferedImage.TYPE_BYTE_GRAY根据你自己的图片来定
    BufferedImage outImg = new BufferedImage(width, height,BufferedImage.TYPE_BYTE_GRAY);
    outImg.getGraphics().drawImage(src, 0, 0, width, height, null);
    FileOutputStream out = new FileOutputStream(localPath);
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    encoder.encode(outImg);
    out.close();
      

  2.   

    没有变小啊。借鉴楼主的:
    import javax.imageio.*;
    import java.awt.image.*;
    public class ImageHandle{
    public static void main(String[]args)throws Exception{
    BufferedImage bin=ImageIO.read(new File("c:\\1.jpg"));
    System.out.println("heigth1: "+bin.getHeight());
    System.out.println("width1: "+bin.getWidth());
    ImageIO.write(bin,"jpg",new File("c:\\2.jpg"));
    bin=ImageIO.read(new File("c:\\2.jpg"));
    System.out.println("heigth2: "+bin.getHeight());
    System.out.println("width2: "+bin.getWidth());
    }
    }输出:
    heigth1: 570
    width1: 760
    heigth2: 570
    width2: 760