2014-10-07 3 views
0

썸네일 이미지 폭 자바 (해상도 변경)내가 thunbnail 이미지를 만들고 싶어

이 내 코드를 만들

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


    } 

가끔 내 이미지 색상이 이미지 예를

예기치 않은 색상 로 변경

origin image

thumbnail imagee

첫 번째는 기원

초 축소판

내가 왜 ... 내가 실수 곳 몰라입니까 ??

도움 제발 ...

+1

관련 : 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

답변

2

당신은 BufferedImage.TYPE_INT_RGB를 사용하여 이미지를 작성합니다. 그러나 소스 이미지의 유형이 아닌 경우 색이 잘못됩니다.

이가이 라인

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

를 교체하십시오 :

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

나는 시도했지만 그 이미지는 .. – developupupup

+0

다음의 이미지 형식은 원본 이미지가 ImageIO.read()에 의해 올바르게 인식되지 않습니다 - 실행하기 전에 원본 이미지를 RGB 형식으로 변환 해보십시오 Java 프로그램 – UniversE

관련 문제