low level programmer

« 好文推薦 - Capability... | Main | JAI - 旋轉效果 ( rotate... »
星期三 八月 13, 2008

JAI - 剪圖效果 (crop)

description

剪下指定區塊的效果

reference

jaistuff

codes

重點在 doCrop 這個 method, 所以放到最前面. IMG_FILE_1 放 classpath 即可.
public class JAITester {

    private static final String IMG_FILE_1 = "test1.jpg";

    private PlanarImage doCrop(PlanarImage imageToCrop, float x, float y, float w, float h) {
        ParameterBlock cropParam = new ParameterBlock();
        cropParam.addSource(imageToCrop);
        cropParam.add(x);
        cropParam.add(y);
        cropParam.add(w);
        cropParam.add(h);
        return JAI.create("crop", cropParam, null);
    }

    /** 
     * http://forums.java.net/jive/message.jspa?messageID=221209
     * this gets rid of exception for not using native acceleration */
    static {
        System.setProperty("com.sun.media.jai.disableMediaLib", "true");
    }

    public static void main(String[] args) {
        JAITester test = new JAITester();
        test.test();
    }

    private void test() {
        PlanarImage input = JAI.create("fileload", IMG_FILE_1);
        JPanel jPanelMain = new JPanel(new MigLayout("", "grow", "grow"));
        jPanelMain.add(new JScrollPane(new DisplayJAI(input)), "grow");

        float x = input.getWidth() / 2;
        float y = input.getHeight() / 2;
        float w = input.getWidth() / 2;
        float h = input.getHeight() / 2;
        PlanarImage output = doCrop(input, x, y, w, h);

        jPanelMain.add(new JScrollPane(new DisplayJAI(output)), "grow");
        show(jPanelMain);
    }

    private void show(JComponent comp) {
        JFrame f = new JFrame();
        f.setLocation(50, 50);
        f.setPreferredSize(new Dimension(800, 600));
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.getContentPane().add(comp);
        f.pack();
        f.setVisible(true);
    }
}

迴響:

發表迴響:
  • HTML 語法: 關閉