Exif信息 处理图片上传时翻转问题

2024-04-26 07:08

本文主要是介绍Exif信息 处理图片上传时翻转问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

java处理旋转角度的图片

Java读取图片EXIF信息的方法:

首先介绍一下什么是EXIF,EXIF是Exchangeable Image File的缩写,这是一种专门为数码相机照片设定的格式。这种格式可以用来记录数字照片的属性信息,例如相机的品牌及型号、相片的拍摄时间、拍摄时所设置 的光圈大小、快门速度、ISO等等信息。除此之外它还能够记录拍摄数据,以及照片格式化方式,这样就可以输出到兼容EXIF格式的外设上,例如照片打印机 等。

目前最常见的支持EXIF信息的图片格式是JPG,很多的图像工具都可以直接显示图片的EXIF信息,包括现在的一些著名的相册网站也提供页面用于 显示照片的EXIF信息。本文主要介绍Java语言如何读取图像的EXIF信息,包括如何根据EXIF信息对图像进行调整以适合用户浏览。

目前最简单易用的EXIF信息处理的Java包是Drew Noakes写的metadata-extractor,需要注意的是,并不是每个JPG图像文件都包含有EXIF信息,你可以在Windows资源管理器单击选中图片后,如果该图片包含EXIF信息,则会在属性->摘要中显示出来。

首先导入依赖,高版本已不支持,

    <dependency><groupId>com.drewnoakes</groupId><artifactId>metadata-extractor</artifactId><version>2.4.0-beta-1</version></dependency>

打包需要注意依赖无法打入需要手动打入

     <plugin><artifactId>maven-compiler-plugin</artifactId><configuration><encoding>UTF-8</encoding><compilerArguments><verbose /><bootclasspath>${java.home}\lib\rt.jar${path.separator}${java.home}\lib\jce.jar${path.separator}</bootclasspath></compilerArguments></configuration></plugin>

旋转工具类

下面展示处理代码工具类`。

public class RotateImageUtil {/**** @param bufferedImage*            图片* @param angel*            旋转角度* @return*/public static BufferedImage rotateImage(BufferedImage bufferedImage, int angel,int imageWidth ,int imageHeight ) {if (bufferedImage == null) {return null;}if (angel < 0) {// 将负数角度,纠正为正数角度angel = angel + 360;}// 计算重新绘制图片的尺寸Rectangle rectangle = calculatorRotatedSize(new Rectangle(new Dimension(imageWidth, imageHeight)), angel);// 获取原始图片的透明度int type = bufferedImage.getColorModel().getTransparency();BufferedImage newImage = null;newImage = new BufferedImage(rectangle.width, rectangle.height, type);Graphics2D graphics = newImage.createGraphics();// 平移位置graphics.translate((rectangle.width - imageWidth) / 2, (rectangle.height - imageHeight) / 2);// 旋转角度graphics.rotate(Math.toRadians(angel), imageWidth / 2, imageHeight / 2);// 绘图graphics.drawImage(bufferedImage, null, null);return newImage;}/*** 旋转图片** @param image*            图片* @param angel*            旋转角度* @return*/public static BufferedImage rotateImage(Image image, int angel) {if (image == null) {return null;}if (angel < 0) {// 将负数角度,纠正为正数角度angel = angel + 360;}int imageWidth = image.getWidth(null);int imageHeight = image.getHeight(null);Rectangle rectangle = calculatorRotatedSize(new Rectangle(new Dimension(imageWidth, imageHeight)), angel);BufferedImage newImage = null;newImage = new BufferedImage(rectangle.width, rectangle.height, BufferedImage.TYPE_INT_RGB);Graphics2D graphics = newImage.createGraphics();// transformgraphics.translate((rectangle.width - imageWidth) / 2, (rectangle.height - imageHeight) / 2);graphics.rotate(Math.toRadians(angel), imageWidth / 2, imageHeight / 2);graphics.drawImage(image, null, null);return newImage;}/*** 计算旋转后的尺寸** @param src* @param angel* @return*/private static Rectangle calculatorRotatedSize(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 java.awt.Rectangle(new Dimension(des_width, des_height));}/*** 获取网络图片流(附加)** @param url* @return*/public static InputStream getImageStream(String url) {try {HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();connection.setReadTimeout(5000);connection.setConnectTimeout(5000);connection.setRequestMethod("GET");if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {InputStream inputStream = connection.getInputStream();return inputStream;}} catch (IOException e) {e.printStackTrace();}return null;}
}

具体使用

其中的file是指上传的MultipartFile

我们将MultipartFile写入了一个临时文件中

对临时文件获取了其exif信息,根据exif信息确定了旋转的

然后对临时文件旋转之后又再次写入临时文件中

        File file2 =  multipartFileToFile(multipartFile); //获取文件流BufferedImage originalImg = ImageIO.read(multipartFile.getInputStream());int imageWidth = originalImg.getWidth();int imageHeight = originalImg.getHeight();Metadata metadata = JpegMetadataReader.readMetadata(file2);Directory directory = metadata.getDirectory(ExifDirectory.class);if(directory.containsTag(ExifDirectory.TAG_ORIENTATION)){ // Exif信息中有保存方向,把信息复制到缩略图int orientation = 0; // 原图片的方向信息orientation = directory.getInt(ExifDirectory.TAG_ORIENTATION);if(6 == orientation) {//翻转了90°和270°的,长宽对调      imageWidth = originalImg.getHeight();//上传图片的宽imageHeight = originalImg.getWidth(); //上传图片的高BufferedImage newImage = RotateImageUtil.rotateImage(originalImg,90,imageHeight,imageWidth);inputStream = bufferedImageToInputStream(newImage);}else if(orientation==0||orientation==1) {  //360;  } else if(orientation==3) {  //180;  }else if(orientation==8) {  //270;  }  }

这篇关于Exif信息 处理图片上传时翻转问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/936905

相关文章

Web服务器-Nginx-高并发问题

《Web服务器-Nginx-高并发问题》Nginx通过事件驱动、I/O多路复用和异步非阻塞技术高效处理高并发,结合动静分离和限流策略,提升性能与稳定性... 目录前言一、架构1. 原生多进程架构2. 事件驱动模型3. IO多路复用4. 异步非阻塞 I/O5. Nginx高并发配置实战二、动静分离1. 职责2

解决升级JDK报错:module java.base does not“opens java.lang.reflect“to unnamed module问题

《解决升级JDK报错:modulejava.basedoesnot“opensjava.lang.reflect“tounnamedmodule问题》SpringBoot启动错误源于Jav... 目录问题描述原因分析解决方案总结问题描述启动sprintboot时报以下错误原因分析编程异js常是由Ja

基于C#实现PDF转图片的详细教程

《基于C#实现PDF转图片的详细教程》在数字化办公场景中,PDF文件的可视化处理需求日益增长,本文将围绕Spire.PDFfor.NET这一工具,详解如何通过C#将PDF转换为JPG、PNG等主流图片... 目录引言一、组件部署二、快速入门:PDF 转图片的核心 C# 代码三、分辨率设置 - 清晰度的决定因

Python自动化处理PDF文档的操作完整指南

《Python自动化处理PDF文档的操作完整指南》在办公自动化中,PDF文档处理是一项常见需求,本文将介绍如何使用Python实现PDF文档的自动化处理,感兴趣的小伙伴可以跟随小编一起学习一下... 目录使用pymupdf读写PDF文件基本概念安装pymupdf提取文本内容提取图像添加水印使用pdfplum

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

MySQL 表空却 ibd 文件过大的问题及解决方法

《MySQL表空却ibd文件过大的问题及解决方法》本文给大家介绍MySQL表空却ibd文件过大的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录一、问题背景:表空却 “吃满” 磁盘的怪事二、问题复现:一步步编程还原异常场景1. 准备测试源表与数据

基于Redis自动过期的流处理暂停机制

《基于Redis自动过期的流处理暂停机制》基于Redis自动过期的流处理暂停机制是一种高效、可靠且易于实现的解决方案,防止延时过大的数据影响实时处理自动恢复处理,以避免积压的数据影响实时性,下面就来详... 目录核心思路代码实现1. 初始化Redis连接和键前缀2. 接收数据时检查暂停状态3. 检测到延时过

Java利用@SneakyThrows注解提升异常处理效率详解

《Java利用@SneakyThrows注解提升异常处理效率详解》这篇文章将深度剖析@SneakyThrows的原理,用法,适用场景以及隐藏的陷阱,看看它如何让Java异常处理效率飙升50%,感兴趣的... 目录前言一、检查型异常的“诅咒”:为什么Java开发者讨厌它1.1 检查型异常的痛点1.2 为什么说

解决Nginx启动报错Job for nginx.service failed because the control process exited with error code问题

《解决Nginx启动报错Jobfornginx.servicefailedbecausethecontrolprocessexitedwitherrorcode问题》Nginx启... 目录一、报错如下二、解决原因三、解决方式总结一、报错如下Job for nginx.service failed bec