图片旋转的代码

来源:互联网 发布:在职医学类研究生知乎 编辑:程序博客网 时间:2024/04/30 21:01


** *//**

 

 

 

     * 旋转图片为指定角度

 

     *

 

 

04

     * @param bufferedimage

 

 

05

     *            目标图像

 

 

06

     * @param degree

 

 

07

     *            旋转角度

 

 

08

     * @return

 

 

09

     */

 

 

10

    public static BufferedImage rotateImage(final BufferedImage bufferedimage,

 

 

11

            final int degree) ...{

 

 

12

        int w = bufferedimage.getWidth();

 

 

13

        int h = bufferedimage.getHeight();

 

 

14

        int type = bufferedimage.getColorModel().getTransparency();

 

 

15

        BufferedImage img;

 

 

16

        Graphics2D graphics2d;

 

 

17

        (graphics2d = (img = new BufferedImage(w, h, type))

 

 

18

                .createGraphics()).setRenderingHint(

 

 

19

                RenderingHints.KEY_INTERPOLATION,

 

 

20

                RenderingHints.VALUE_INTERPOLATION_BILINEAR);

 

 

21

        graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);

 

 

22

        graphics2d.drawImage(bufferedimage, 0, 0, null);

 

 

23

        graphics2d.dispose();

 

 

24

        return img;

 

 

25

    }

 

 

26

 

 

 

27

    /** *//**

 

 

28

     * 变更图像为指定大小

 

 

29

     *

 

 

30

     * @param bufferedimage

 

 

31

     *            目标图像

 

 

32

     * @param w

 

 

33

     *            宽

 

 

34

     * @param h

 

 

35

     *            高

 

 

36

     * @return

 

 

37

     */

 

 

38

    public static BufferedImage resizeImage(final BufferedImage bufferedimage,

 

 

39

            final int w, final int h) ...{

 

 

40

        int type = bufferedimage.getColorModel().getTransparency();

 

 

41

        BufferedImage img;

 

 

42

        Graphics2D graphics2d;

 

 

43

        (graphics2d = (img = createImage(w, h, type))

 

 

44

                .createGraphics()).setRenderingHint(

 

 

45

                RenderingHints.KEY_INTERPOLATION,

 

 

46

                RenderingHints.VALUE_INTERPOLATION_BILINEAR);

 

 

47

        graphics2d.drawImage(bufferedimage, 0, 0, w, h, 0, 0, bufferedimage

 

 

48

                .getWidth(), bufferedimage.getHeight(), null);

 

 

49

        graphics2d.dispose();

 

 

50

        return img;

 

 

51

    }

 

 

52

 

 

 

53

    /** *//**

 

 

54

     * 水平翻转图像

 

 

55

     *

 

 

56

     * @param bufferedimage 目标图像

 

 

57

     * @return

 

 

58

     */

 

 

59

    public static BufferedImage flipImage(final BufferedImage bufferedimage) ...{

 

 

60

        int w = bufferedimage.getWidth();

 

 

61

        int h = bufferedimage.getHeight();

 

 

62

        BufferedImage img;

 

 

63

        Graphics2D graphics2d;

 

 

64

        (graphics2d = (img = createImage(w, h, bufferedimage

 

 

65

                .getColorModel().getTransparency())).createGraphics())

 

 

66

                .drawImage(bufferedimage, 0, 0, w, h, w, 0, 0, h, null);

 

 

67

        graphics2d.dispose();

 

 

68

        return img;

 

 

69

    }

文章出处:飞诺网(www.diybl.com):http://www.diybl.com/course/3_program/java/javajs/200823/98524.html

原创粉丝点击