使用Java裁剪图像

时间:2013-04-20 04:06:09

标签: java image crop

如何在Java中裁剪图像?我目前有这个类用于图像处理。

使用run方法的主要方法:

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

}


任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分: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;
    }

此方法将帮助您裁剪图像。它对我的项目工作正常。希望它能帮到你。