java - 使用 Java 裁剪图像

标签 java image crop

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

main方法与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();
    }

}


任何帮助将不胜感激。

最佳答案

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

关于java - 使用 Java 裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16116448/

相关文章:

java - EWS 和 Exchange 2010 流通知 - 看到双重

java - 如何查看我的 Java 小程序的错误消息?

javascript - 拖放期间从外部远程图像读取 URL 数据

css - 响应式垂直对齐向右浮动图像和文本

android - Google 相册应用程序风格的图像裁剪、拉直和平移

iOS 音频修剪

java - 看似无关的代码块被注释掉时出现 OutOfMemoryError

java - 如何从现有的 java.util.Date 对象中单独获取时间和日期部分

css - 将图像与表格单元格的边缘对齐

facebook - 像 Flickr 和 Facebook 照片流一样将照片大小调整为缩略图方 block ?