low level programmer

« JAI - 剪圖效果 (crop) | Main | JAI - 縮放後平移效果 (... »
星期三 八月 13, 2008

JAI - 旋轉效果 ( rotate )

description

從指定的 (x, y) 為圓心旋轉指定的角度.

reference

jaistuff

codes

重點在 doRotate 這個 method. 注意我們習慣的 45 度是 degree, 必須轉成 radian 才正確.
public class JAITester {

    private static final float  CENTRAL_X  = 45;
    private static final float  CENTRAL_Y  = 45;
    private static final float  DEGREES    = 45;
    private static final String IMG_FILE_1 = "test1.jpg";

    private PlanarImage doRotate(PlanarImage imageToCrop) {
        float angle = (float) Math.toRadians(DEGREES);
        ParameterBlock rotateParam = new ParameterBlock();
        rotateParam.addSource(imageToCrop);
        rotateParam.add(CENTRAL_X);
        rotateParam.add(CENTRAL_Y);
        rotateParam.add( angle );
        rotateParam.add( new InterpolationBilinear() );
        return JAI.create( "rotate", rotateParam  );
    }
    
    /** 
     * 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");
        jPanelMain.add(new JScrollPane(new DisplayJAI(doRotate(input))), "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 語法: 關閉