2013-04-20 3 views
1

Java에서 이미지를 자르는 방법은 무엇입니까? 현재 이미지 조작을위한 클래스가 있습니다.Java를 사용하여 이미지 자르기

실행 방법의 주요 방법 :

public static void main(String[] args) { 
    GameProject gp = new GameProject(); 
    gp.run(); 
} 

public void run(){ 
    s = new Screen(); 

    try { 
     DisplayMode dm = s.findFirstCompatibleMode(modes); 
     s.setFullscreen(dm); 
     Fonts f = new Fonts(); //Checks if certain fonts exsist, if not install them. 
     Images i = new Images(); 

     try { 
      i.draw("H:\\Dropbox\\Dropbox\\GameProject\\src\\Resources\\brock.png", 200, 200); 
      Thread.sleep(50000); 
     } catch (Exception e) {} 
    } finally { 
     s.restoreScreen(); 
    } 
} 


이미지 클래스 : 어떤 도움을 주시면 감사하겠습니다

package Handlers; 

import javax.swing.ImageIcon; 
import java.awt.*; 

/** 
* 
* @author Steven 
*/ 
public class Images { 

    /** 
    * @param args the command line arguments 
    */ 
    private Screen s; 

    public void draw(String name, int x, int y) { //Draws the image 
     s = new Screen(); 
     Graphics2D g = s.getGraphics(); 
     draws(g, name, x, y, getWidth(name), getHeight(name)); 
    } 

    public void drawA(String name, int x, int y){ //Draws the image, allows for a more advanced image manipulation 
     s = new Screen(); 
     Graphics2D g = s.getGraphics(); 
     draws(g, name, x, y, getWidth(name), getHeight(name)); 
    }  

    public void draws(Graphics g, String name, int x, int y, int w, int h) { //Draws and updates the screen 
     s = new Screen(); 
     g.drawImage(new ImageIcon(name).getImage(), x, y, w, h, null); 
     s.update(); 
    } 

    public int getWidth(String name) { //Gets the image width 
     return new ImageIcon(name).getIconWidth(); 
    } 

    public int getHeight(String name) { //Gets the images height 
     return new ImageIcon(name).getIconHeight(); 
    } 

} 


.

+0

더 나은 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. SSCCE가 되려면 코드에 이미지를 생성하거나 하나의 이미지에 대한 핫 링크를 생성하는'main (String []) '이 필요합니다. –

+0

당신은'Graphics2D'의'.getSubimage()'를 사용할 것입니다. 나는 그것을 사용하는 "그림 스크램블러"퍼즐을 만들기 위해 자바 클래스에 대한 강의를 가졌습니다. 이 메소드가'Graphics2D' 또는'BufferedImage'에서만 나온 것인지 기억하지 못합니다. – Kroltan

+0

나는 BufferedImage에서 그것을 생각한다. –

답변

4

CropImageFilter을 사용하여 이미지를자를 수 있습니다. 또한, java.awt.image 패키지를 살펴보면 이미지 조작 루틴이 많이 있습니다.

+0

감사합니다. 나는 그것을 살펴볼 것입니다. –

+0

내 대답이 마음에 들면 받아 들여야합니다. –

+0

java.awt.image가 도움이 되었기 때문에, 내가 찾고 있던 효과를 제공하는 메소드를 찾을 수있었습니다. –

1

난 내 자신의 프로젝트에서이를 사용했다 : -

public boolean CropImage(int cropHeight,int cropWidth,int windowLeft,int windowTop,File srcFile,String destDirectory,String destFilename,int commonPadding,String fileFormat,HttpServletRequest request)throws IOException{ 
     boolean isOkResolution=false; 
     try { 
      String dirPath=request.getRealPath("")+"/"+destDirectory; 
      File f=new File(dirPath); 
      if(!f.isDirectory()){ 
       f.mkdir(); 
      } 
      String destpath=dirPath+"/"+destFilename; 
      File outputFile=new File(destpath); 
      FileInputStream fis=new FileInputStream(srcFile); 
      BufferedImage bimage=ImageIO.read(fis); 
      System.out.println("Image Origilnal Height=="+bimage.getHeight()); 
      BufferedImage oneimg=new BufferedImage(cropHeight,cropWidth,bimage.getType()); 
      Graphics2D gr2d=oneimg.createGraphics(); 
      isOkResolution=gr2d.drawImage(bimage,0,0,cropWidth,cropHeight,windowLeft-commonPadding,windowTop-commonPadding,(windowLeft+cropWidth)-commonPadding,(windowTop+cropHeight)-commonPadding,null); 
      gr2d.dispose(); 
      ImageIO.write(oneimg,fileFormat,outputFile); 
     } catch (FileNotFoundException fe) { 
      System.out.println("No File Found =="+fe); 
     } catch(Exception ex){ 
      System.out.println("Error in Croping File="+ex); 
      ex.printStackTrace(); 
     } 
     return isOkResolution; 
    } 

이 방법은 당신을 도울 것이다 당신이 내 project.Hope에 대한 벌금을 작업 image.Its를 잘라하는 데 도움이 될 것입니다.

+0

참고 ^를 계속 사용 하겠지만 이미지를 더 작은 버전으로 바꾸는 것이 아닙니다. 나는 게임을 개발하려고 노력하고 있는데, 나는 한 가지 이미지의 절반을 사용하는 방법을 배우고 싶다. 그런 다음 특정 이벤트가 발생하면 다른 절반 (예 : npcs 걷기)으로 갈 것이다. –

+0

일반적으로 필요한 세부 사항 만 사용하여 요약 코드를 읽는 것이 더 편합니다. – Zon

관련 문제