0

i wanna make a thunbnail image(resized image)

this is my code

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);


    }

sometimes my image colors are changed with unexpected colors this is image example

origin image

thumbnail imagee

first is origin

second is thumbnails

i don't know why... where i mistake??

help me please...

  • 1
    Related: http://stackoverflow.com/questions/13072312/jpeg-image-color-gets-drastically-changed-after-just-imageio-read-and-imageio , http://stackoverflow.com/questions/4386446/problem-using-imageio-write-jpg-file – Marco13 Oct 07 '14 at 09:31

1 Answers1

2

You write your image using BufferedImage.TYPE_INT_RGB. But if this is not the type of the source image, the colors get wrong.

Try replacing this line

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

by this:

BufferedImage thu = new BufferedImage(width, height, bi.getType());
UniversE
  • 2,419
  • 17
  • 24
  • then the image format of the source image is not recognized correctly by ImageIO.read() - try converting the source image to RGB-Format before running the java program – UniversE Oct 07 '14 at 09:48