2013-03-11 2 views
2

Java를 사용하여 한 위치에서 다른 위치로 이미지 파일을 복사하려고합니다. 이제 소스 위치에있는 이미지 파일의 크기가 무엇이든 특정 크기의 이미지 파일을 저장하려고합니다.다른 크기의 다른 위치로 이미지 복사

나는 소스 파일과 같은 크기의 대상 위치에 이미지를 생산하고, 다음 코드를 사용하고 있습니다 :

여기
public class filecopy { 
    public static void copyFile(File sourceFile, File destFile) 
      throws IOException { 
     if (!destFile.exists()) { 
      destFile.createNewFile(); 
     } 

     FileChannel source = null; 
     FileChannel destination = null; 
     try { 
      source = new FileInputStream(sourceFile).getChannel(); 
      destination = new FileOutputStream(destFile).getChannel(); 

      // previous code: destination.transferFrom(source, 0, source.size()); 
      // to avoid infinite loops, should be: 
      long count = 0; 
      long size = source.size(); 
      while ((count += destination.transferFrom(source, count, size 
        - count)) < size) 
       ; 
     } finally { 
      if (source != null) { 
       source.close(); 
      } 
      if (destination != null) { 
       destination.close(); 
      } 
     } 
    } 

    public static void main(String args[]) { 
     try { 
     File sourceFile = new File("D:/new folder/abc.jpg"); 
     File destFile = new File("d:/new folder1/abc.jpg"); 
     copyFile(sourceFile,destFile); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 
+1

좋은 프로젝트, 나는 항상 저도 그렇게하고 싶었습니다. 그러나 당신의 질문은 무엇입니까? – ppeterka

+1

참고로, 코드는 컴파일 할 수 없습니다 (마지막에 매달려있는 문자열이 있음). – Perception

답변

4

귀하의 사양에 따라 이미지의 크기를 조절하기위한 코드입니다. copyFile 메서드 내에서

int width=100,height=75; /* set the width and height here */ 
BufferedImage inputImage=ImageIO.read(sourceFile); 
BufferedImage outputImage=new BufferedImage(width, height, 
    BufferedImage.TYPE_INT_RGB); 
Graphics2D g=outputImage.createGraphics(); 
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
    RenderingHints.VALUE_INTERPOLATION_BICUBIC); 
g.clearRect(0, 0, width, height); 
g.drawImage(inputImage, 0, 0, width, height, null); 
g.dispose(); 
ImageIO.write(outputImage,"jpg",destFile); 
/* first parameter is the object of the BufferedImage, 
    second parameter is the type of image that you are going to write, 
     you can use jpg, bmp, png etc 
    third parameter is the destination file object. */ 
+0

주어진 코드를 컴파일 한 후 예를 들어 제안 파일을 크기를 조정하려고합니다. –

+0

@adeshsingh도 관련 패키지를 가져옵니다. 이것이 당신이 필요로하는 것이되기를 바랍니다. – Maximin