JAVA用Graphics2D实现图片旋转,缩放,合成

第一步:读入文件

public BufferedImage loadImageLocal(String path) {  
        try {  
            return ImageIO.read(new File(path));  
        } catch (IOException e) {  
            e.printStackTrace();
        }  
        return null;  
    } 

第二步:缩放文件

/**
     * 
     * @param mini 贴图
     * @param Scale 缩放比例
     * @return
     */
public BufferedImage modifyImageScale(BufferedImage mini,double Scale) {
        int w = (int)Math.round(mini.getWidth()*Scale);
        int h = (int)Math.round(mini.getHeight()*Scale);
        //设置生成图片宽*高,色彩
        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        //创建画布
        Graphics2D g2 = bi.createGraphics();
        //设置图片透明  注********:只有png格式的图片才能设置背景透明,jpg设置图片颜色变的乱七八糟
        bi = g2.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);
        //重新创建画布
        g2 = bi.createGraphics();
        //画图
        g2.drawImage(mini, 0,0,w,h, null);
        //关闭资源
        g2.dispose();
        return bi;
    }

第三步:旋转文件

/**
     * 
     * @param mini 贴图
     * @param ratio  旋转角度
     * @return
     */
public BufferedImage modifyImageRatio(BufferedImage mini,int ratio) {
    	 int src_width = mini.getWidth();  
         int src_height = mini.getHeight();  
         //针对图片旋转重新计算图的宽*高
         Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(  
                 src_width, src_height)), ratio);  
         //设置生成图片的宽*高,色彩度
         BufferedImage res = new BufferedImage(rect_des.width, rect_des.height,BufferedImage.TYPE_INT_RGB);  
         //创建画布
         Graphics2D g2 = res.createGraphics();  
         res = g2.getDeviceConfiguration().createCompatibleImage(rect_des.width, rect_des.height, Transparency.TRANSLUCENT);
         g2 = res.createGraphics();
         //重新设定原点坐标
         g2.translate((rect_des.width - src_width) / 2,  
                 (rect_des.height - src_height) / 2);  
         //执行图片旋转,rotate里包含了translate,并还原了原点坐标
         g2.rotate(Math.toRadians(ratio), src_width / 2, src_height / 2);  
         g2.drawImage(mini, null, null);  
         g2.dispose();
        return res;
    }
    
 private Rectangle CalcRotatedSize(Rectangle src, int angel) {  
        if (angel >= 90) {  
            if(angel / 90 % 2 == 1){  
                int temp = src.height;  
                src.height = src.width;  
                src.width = temp;  
            }  
            angel = angel % 90;  
        }  
  
        double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;  
        double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;  
        double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;  
        double angel_dalta_width = Math.atan((double) src.height / src.width);  
        double angel_dalta_height = Math.atan((double) src.width / src.height);  
  
        int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha  
                - angel_dalta_width));  
        int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha  
                - angel_dalta_height));  
        int des_width = src.width + len_dalta_width * 2;  
        int des_height = src.height + len_dalta_height * 2;  
        return new Rectangle(new Dimension(des_width, des_height));  
    }  

第四步:合成文件

/**
	 * 
	 * @param mini 贴图
	 * @param big  底图
	 * @param indexX x坐标位置
	 * @param indexY y坐标位置
	 * @return
	 */
    public BufferedImage modifyImagetogeter(BufferedImage mini, BufferedImage big,Integer indexX,Integer indexY) {  
        try {  
        	int w =mini.getWidth();
        	int h =mini.getHeight();
        	System.out.println(w+" , "+h);
        	Graphics2D g = big.createGraphics();  
            g.drawImage(mini, indexX, indexY, w,h, null); 
            g.dispose();  
        } catch (Exception e) {  
            System.out.println(e.getMessage());  
        }  
  
        return big;  
    } 

第五步:导出文件

 public void writeImageLocal(String newPath, BufferedImage newImg) {  
        if (newPath != null && newImg != null) {  
            try {  
                File outputfile = new File(newPath);  
                ImageIO.write(newImg, "png", outputfile);  
            } catch (IOException e) {  
                System.out.println(e.getMessage());  
            }  
        }  
    } 
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用JavaGraphics2D类压图片,你可以按照以下步骤: 1. 加载要压图片,可以使用ImageIO类: ```java BufferedImage originalImage = ImageIO.read(new File("path/to/image")); ``` 2. 创建一个新的BufferedImage对象,设置它的宽度和高度为原始图像的一半,或者其他你想要的压比例: ```java int originalWidth = originalImage.getWidth(); int originalHeight = originalImage.getHeight(); int newWidth = originalWidth / 2; // 压一半 int newHeight = originalHeight / 2; // 压一半 BufferedImage compressedImage = new BufferedImage(newWidth, newHeight, originalImage.getType()); ``` 3. 获取Graphics2D对象,用它来绘制压后的图像: ```java Graphics2D g2d = compressedImage.createGraphics(); g2d.drawImage(originalImage, 0, 0, newWidth, newHeight, null); g2d.dispose(); ``` 4. 最后,将压后的图像保存到文件或输出流中,可以使用ImageIO类: ```java ImageIO.write(compressedImage, "jpg", new File("path/to/compressed/image.jpg")); ``` 完整的代码: ```java import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ImageCompressor { public static void main(String[] args) throws Exception { BufferedImage originalImage = ImageIO.read(new File("path/to/image")); int originalWidth = originalImage.getWidth(); int originalHeight = originalImage.getHeight(); int newWidth = originalWidth / 2; // 压一半 int newHeight = originalHeight / 2; // 压一半 BufferedImage compressedImage = new BufferedImage(newWidth, newHeight, originalImage.getType()); Graphics2D g2d = compressedImage.createGraphics(); g2d.drawImage(originalImage, 0, 0, newWidth, newHeight, null); g2d.dispose(); ImageIO.write(compressedImage, "jpg", new File("path/to/compressed/image.jpg")); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值