2013-04-20 177 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 [])',它可以生成一个代码图像或者热链接到一个图像。 – 2013-04-20 04:09:24

+0

我想你会使用'Graphics2D'的'.getSubimage()'。我在我的java课上有一堂课来制作一个“图片扰乱者”的谜题,我在那里使用它。不过,不记得这个方法是来自'Graphics2D'还是仅仅''BufferedImage'。 – Kroltan 2013-04-20 04:12:03

+0

我认为它在BufferedImage中。 – 2013-04-20 04:33:08

回答

4

您可以使用CropImageFilter来裁剪图像。另外,请看一下java.awt.image包,它有很多图像处理例程。

+0

谢谢,我要去看看它。 – 2013-04-20 04:33:28

+0

如果您喜欢我的回答,请将其标记为已接受。 – 2013-04-20 15:12:33

+0

java.awt.image是帮助,我设法找到一种方法,其中给我我正在寻找的效果。 – 2013-04-20 15:47:54

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)。 – 2013-04-20 15:01:00

+0

通常阅读杂交代码比较舒服,只需要详细信息。 – Zon 2013-10-26 12:27:20