获取图片的旋转角度信息

//通过摄像头拍摄的图片才有exif信息
getOrientation(file) { 
 return new Promise((resolve, reject) => {   
     const img = new Image();    
     img.src = window.URL.createObjectURL(file);    
     img.onload = () => {      
        // 获取图片元数据(EXIF 变量是引入的 exif-js 库暴露的全局变量)     
        exif.EXIF.getData(img, function () {        
        // 获取图片旋转标志位        
        let orientation = exif.EXIF.getTag(this, "Orientation");        
        resolve(orientation || 1)      
      })    
     }    
    img.onerror = (err) => {      
      reject(err)    
     }  
})}复制代码



转载于:https://juejin.im/post/5cb7dbb3e51d4514df42067a

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取图片旋转度并实现图片角度旋转,可以使用Java的ImageIO和AffineTransform类。以下是一个示例代码: ```java import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ImageRotateExample { public static void main(String[] args) throws Exception { // 读取图片文件 File input = new File("input.png"); BufferedImage image = ImageIO.read(input); // 获取图片旋转度 int angle = getRotateAngle(image); System.out.println("图片旋转度:" + angle); // 旋转图片 BufferedImage rotatedImage = rotateImage(image, angle); // 保存旋转后的图片 File output = new File("output.png"); ImageIO.write(rotatedImage, "png", output); } // 获取图片旋转度 public static int getRotateAngle(BufferedImage image) { // 获取图片的宽度和高度 int width = image.getWidth(); int height = image.getHeight(); // 获取图片的四个角的像素值 int topLeft = image.getRGB(0, 0); int topRight = image.getRGB(width-1, 0); int bottomLeft = image.getRGB(0, height-1); int bottomRight = image.getRGB(width-1, height-1); // 判断图片是否旋转 if (topLeft == topRight && topRight == bottomLeft && bottomLeft == bottomRight) { return 0; } else if (topLeft == bottomRight && topRight == bottomLeft) { return 180; } else if (topLeft == topRight) { return -90; } else { return 90; } } // 旋转图片 public static BufferedImage rotateImage(BufferedImage image, int angle) { // 计算旋转中心点 int centerX = image.getWidth() / 2; int centerY = image.getHeight() / 2; // 创建变换矩阵 AffineTransform transform = new AffineTransform(); transform.rotate(Math.toRadians(angle), centerX, centerY); // 创建变换操作对象 AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR); // 执行变换操作 return op.filter(image, null); } } ``` 在上面的示例代码中,getRotateAngle方法用于获取图片旋转度,rotateImage方法用于旋转图片。这两个方法都使用了AffineTransform类来进行变换操作。注意:以上代码只能处理图片旋转角度为0、90、180、270度的情况。如果需要处理其他旋转角度图片,需要进行更复杂的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值