Java实现图片旋转、指定图像大小和水平翻转
本文实例为大家分享了Java实现图片旋转、指定图像大小、水平翻转,供大家参考,具体内容如下
packagecom.zeph.j2se.image; importjava.awt.Graphics2D; importjava.awt.RenderingHints; importjava.awt.image.BufferedImage; publicclassImageOperate{ /** *旋转图片为指定角度 * *@parambufferedimage *目标图像 *@paramdegree *旋转角度 *@return */ publicstaticBufferedImagerotateImage(finalBufferedImagebufferedimage, finalintdegree){ intw=bufferedimage.getWidth(); inth=bufferedimage.getHeight(); inttype=bufferedimage.getColorModel().getTransparency(); BufferedImageimg; Graphics2Dgraphics2d; (graphics2d=(img=newBufferedImage(w,h,type)).createGraphics()) .setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(degree),w/2,h/2); graphics2d.drawImage(bufferedimage,0,0,null); graphics2d.dispose(); returnimg; } /** *变更图像为指定大小 * *@parambufferedimage *目标图像 *@paramw *宽 *@paramh *高 *@return */ publicstaticBufferedImageresizeImage(finalBufferedImagebufferedimage, finalintw,finalinth){ inttype=bufferedimage.getColorModel().getTransparency(); BufferedImageimg; Graphics2Dgraphics2d; (graphics2d=(img=newBufferedImage(w,h,type)).createGraphics()) .setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.drawImage(bufferedimage,0,0,w,h,0,0, bufferedimage.getWidth(),bufferedimage.getHeight(),null); graphics2d.dispose(); returnimg; } /** *水平翻转图像 * *@parambufferedimage *目标图像 *@return */ publicstaticBufferedImageflipImage(finalBufferedImagebufferedimage){ intw=bufferedimage.getWidth(); inth=bufferedimage.getHeight(); BufferedImageimg; Graphics2Dgraphics2d; (graphics2d=(img=newBufferedImage(w,h,bufferedimage .getColorModel().getTransparency())).createGraphics()) .drawImage(bufferedimage,0,0,w,h,w,0,0,h,null); graphics2d.dispose(); returnimg; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。