【问题标题】:make a thumbnail image width java制作缩略图图像宽度java
【发布时间】:2014-10-07 09:26:12
【问题描述】:

我想做一个缩略图(调整大小的图像)

这是我的代码

public static void createImage(String loadFile, String saveFile)throws IOException{

        File load_image = new File(loadFile); //가져오는거
        FileInputStream fis = new FileInputStream(load_image);
        File save = new File(saveFile); // 썸네일

        BufferedImage bi = ImageIO.read(fis);

         int width = bi.getWidth();
         int height = bi.getHeight();
         int maxWidth=0;
         int maxHeight=0;

         if(width>height){
             maxWidth = 1280;
             maxHeight = 720;
         }else{
             maxWidth = 720;
             maxHeight = 1280;
         }

         if(width > maxWidth){
             float widthRatio = maxWidth/(float)width;
             width = (int)(width*widthRatio);
             height = (int)(height*widthRatio);
         }

         if(height > maxHeight){
             float heightRatio = maxHeight/(float)height;
             width = (int)(width*heightRatio);
             height = (int)(height*heightRatio);
         }


        BufferedImage thu = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = thu.createGraphics();
        g2.drawImage(bi, 0, 0, width, height, null);
        ImageIO.write(thu, "jpg", save);


    }

有时我的图像颜色会因意想不到的颜色而改变 这是图片示例

首先是原点

第二个是缩略图

我不知道为什么... 我哪里错了??

请帮帮我...

【问题讨论】:

标签: java image


【解决方案1】:

您使用BufferedImage.TYPE_INT_RGB 编写图像。但如果这不是源图像的类型,颜色就会出错。

尝试替换此行

BufferedImage thu = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

通过这个:

BufferedImage thu = new BufferedImage(width, height, bi.getType());

【讨论】:

  • 那么 ImageIO.read() 无法正确识别源图像的图像格式 - 在运行 java 程序之前尝试将源图像转换为 RGB 格式
猜你喜欢
  • 2015-03-17
  • 2010-11-07
  • 1970-01-01
  • 1970-01-01
  • 2013-06-20
  • 2012-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多