JAI - 剪圖效果 (crop)
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);
}
}
Posted at 12:06上午 八月 13, 2008 by shooeugenesea in Java Image | 迴響[0]