java实现图像旋转

算法原理icon-default.png?t=M276https://blog.csdn.net/xiaoshen0121/article/details/79428344?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%9B%BE%E5%83%8F%E6%97%8B%E8%BD%AC&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-8-79428344.nonecase&spm=1018.2226.3001.4187

package img;

import javax.imageio.ImageIO;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;

public class main {

    public static void main(String[] args) throws Exception {
        // 读取图片
        BufferedImage bufImage = ImageIO.read(new File("F:\\Files\\javaFile\\Image\\src\\img\\OrignImg.jpg"));

        rotateImage(bufImage,35);
        
        System.out.println("Successfully!");
    }
    public static void rotateImage(BufferedImage image,double degree) throws Exception {
    	
    	degree=Math.toRadians(360-degree);
    	
    	int w = image.getWidth();  
	    int h = image.getHeight();  
	    int new_w=(int) Math.ceil(Math.abs(w*Math.cos(degree)) + Math.abs(h*Math.sin(degree)));
	    int new_h=(int) Math.ceil(Math.abs(h*Math.cos(degree)) + Math.abs(w*Math.sin(degree)));
	    
    	BufferedImage rotImg= new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
    	
    	for (int x = 0; x < new_w; x++) {  
	        for (int y = 0; y < new_h; y++) {  
	            int x1= (int) Math.round(x*Math.cos(degree)+y*Math.sin(degree)-0.5*new_w*Math.cos(degree)-0.5*new_h*Math.sin(degree)+0.5*w);
	            int y1= (int) Math.round(-x*Math.sin(degree)+y*Math.cos(degree)+0.5*new_w*Math.sin(degree)-0.5*new_h*Math.cos(degree)+0.5*h);
            	if(x1>=0&&x1<w&&y1>=0&&y1<h) {
		            rotImg.setRGB(x, y, image.getRGB(x1, y1));
            	}else {
            		rotImg.setRGB(x, y, new Color(255, 255, 255).getRGB());
            	}
            	   
	        }  
	    } 
    	// 把修改过的 rotImg 保存到本地
        ImageIO.write(rotImg, "JPEG", new File("F:\\Files\\javaFile\\Image\\src\\img\\rot.jpg"));
    }
}

实现效果

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值