SpringBoot项目获取图片信息

第一步:引入依赖

<!-- 图片地理信息依赖 -->
<dependency>
    <groupId>com.drewnoakes</groupId>
    <artifactId>metadata-extractor</artifactId>
    <version>2.11.0</version>
</dependency>

第二步:编写代码,获取信息

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.hkwl.hkboot.smp.common.Ret;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;


/**
 * @Description:
 * @Author Be.insighted
 * @Date 2022/4/26 17:29
 */
@RestController
@Slf4j
public class ReadPictureInfoController {

    @GetMapping("picture/info")
    private Ret getPictureInfo() throws Exception {
        printImageTags(new File("D://1.jpg"));
        return Ret.success();
    }


    /**
     * 读取照片里面的信息
     */
    private static void printImageTags(File file) throws ImageProcessingException, Exception {
        Metadata metadata = ImageMetadataReader.readMetadata(file);
        for (Directory directory : metadata.getDirectories()) {
            for (Tag tag : directory.getTags()) {
                String tagName = tag.getTagName();  //标签名
                log.error("标签名: " + tagName);
                String desc = tag.getDescription(); //标签信息
                if (tagName.equals("Image Height")) {
                    System.out.println("图片高度: " + desc);
                } else if (tagName.equals("Image Width")) {
                    System.out.println("图片宽度: " + desc);
                } else if (tagName.equals("Date/Time Original")) {
                    System.out.println("拍摄时间: " + desc);
                } else if (tagName.equals("GPS Latitude")) {
                    System.err.println("纬度 : " + desc);
                    System.err.println("纬度(度分秒格式) : "+pointToLatlong(desc));
                } else if (tagName.equals("GPS Longitude")) {
                    System.err.println("经度: " + desc);
                    System.err.println("经度(度分秒格式): "+pointToLatlong(desc));
                }
            }
        }
    }

    /**
     * 经纬度格式  转换为  度分秒格式 ,如果需要的话可以调用该方法进行转换
     *
     * @param point 坐标点
     * @return
     */
    public static String pointToLatlong(String point) {
        Double du = Double.parseDouble(point.substring(0, point.indexOf("°")).trim());
        Double fen = Double.parseDouble(point.substring(point.indexOf("°") + 1, point.indexOf("'")).trim());
        Double miao = Double.parseDouble(point.substring(point.indexOf("'") + 1, point.indexOf("\"")).trim());
        Double duStr = du + fen / 60 + miao / 60 / 60;
        return duStr.toString();
    }

}

标签项:

2022-04-26 19:28:31.010 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Compression Type
2022-04-26 19:28:31.010 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Data Precision
2022-04-26 19:28:31.010 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Image Height
图片高度: 4344 pixels
2022-04-26 19:28:31.011 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Image Width
图片宽度: 5792 pixels
2022-04-26 19:28:31.011 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Number of Components
2022-04-26 19:28:31.011 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Component 1
2022-04-26 19:28:31.011 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Component 2
2022-04-26 19:28:31.011 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Component 3
2022-04-26 19:28:31.011 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Image Width
图片宽度: 5792 pixels
2022-04-26 19:28:31.011 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Model
2022-04-26 19:28:31.012 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Image Height
图片高度: 4344 pixels
2022-04-26 19:28:31.012 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Orientation
2022-04-26 19:28:31.012 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Date/Time
2022-04-26 19:28:31.012 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: YCbCr Positioning
2022-04-26 19:28:31.012 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Resolution Unit
2022-04-26 19:28:31.012 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: X Resolution
2022-04-26 19:28:31.012 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Y Resolution
2022-04-26 19:28:31.012 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Make
2022-04-26 19:28:31.012 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Unknown tag (0x9aaa)
2022-04-26 19:28:31.013 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: ISO Speed Ratings
2022-04-26 19:28:31.019 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Exposure Program
2022-04-26 19:28:31.019 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: F-Number
2022-04-26 19:28:31.019 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Exposure Time
2022-04-26 19:28:31.020 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Unknown tag (0x9999)
2022-04-26 19:28:31.024 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Sensing Method
2022-04-26 19:28:31.024 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Unknown tag (0x8895)
2022-04-26 19:28:31.024 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Sub-Sec Time Digitized
2022-04-26 19:28:31.024 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Sub-Sec Time Original
2022-04-26 19:28:31.024 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Sub-Sec Time
2022-04-26 19:28:31.024 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Focal Length
2022-04-26 19:28:31.024 ERROR 纬度 : 0° 0' 0"
纬度(度分秒格式) : 0.0
经度: 0° 0' 0"
经度(度分秒格式): 0.0
20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Flash
2022-04-26 19:28:31.024 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: White Balance
2022-04-26 19:28:31.024 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Metering Mode
2022-04-26 19:28:31.025 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Scene Capture Type
2022-04-26 19:28:31.025 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Focal Length 35
2022-04-26 19:28:31.025 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Max Aperture Value
2022-04-26 19:28:31.025 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Date/Time Digitized
2022-04-26 19:28:31.025 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Exposure Bias Value
2022-04-26 19:28:31.025 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Exif Image Height
2022-04-26 19:28:31.025 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: White Balance Mode
2022-04-26 19:28:31.025 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Date/Time Original
拍摄时间: 2022:04:24 18:23:22
2022-04-26 19:28:31.026 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Brightness Value
2022-04-26 19:28:31.026 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Exif Image Width
2022-04-26 19:28:31.026 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Exposure Mode
2022-04-26 19:28:31.026 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Aperture Value
2022-04-26 19:28:31.026 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Components Configuration
2022-04-26 19:28:31.026 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Color Space
2022-04-26 19:28:31.026 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Scene Type
2022-04-26 19:28:31.026 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Shutter Speed Value
2022-04-26 19:28:31.026 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Exif Version
2022-04-26 19:28:31.027 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: FlashPix Version
2022-04-26 19:28:31.027 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Interoperability Index
2022-04-26 19:28:31.027 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Interoperability Version
2022-04-26 19:28:31.027 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: GPS Latitude Ref
2022-04-26 19:28:31.027 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: GPS Latitude
2022-04-26 19:28:31.027 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: GPS Longitude Ref
2022-04-26 19:28:31.027 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: GPS Longitude
2022-04-26 19:28:31.028 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: GPS Altitude Ref
2022-04-26 19:28:31.029 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: GPS Altitude
2022-04-26 19:28:31.029 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: GPS Time-Stamp
2022-04-26 19:28:31.029 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: GPS Processing Method
2022-04-26 19:28:31.029 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: GPS Date Stamp
2022-04-26 19:28:31.029 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Thumbnail Offset
2022-04-26 19:28:31.029 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Orientation
2022-04-26 19:28:31.029 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Thumbnail Length
2022-04-26 19:28:31.030 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Compression
2022-04-26 19:28:31.031 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Resolution Unit
2022-04-26 19:28:31.031 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: X Resolution
2022-04-26 19:28:31.031 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Y Resolution
2022-04-26 19:28:31.031 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Exif Image Height
2022-04-26 19:28:31.031 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Exif Image Width
2022-04-26 19:28:31.031 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: XMP Value Count
2022-04-26 19:28:31.031 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Number of Tables
2022-04-26 19:28:31.031 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Detected File Type Name
2022-04-26 19:28:31.031 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Detected File Type Long Name
2022-04-26 19:28:31.031 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Detected MIME Type
2022-04-26 19:28:31.031 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: Expected File Name Extension
2022-04-26 19:28:31.032 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: File Name
2022-04-26 19:28:31.032 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: File Size
2022-04-26 19:28:31.032 ERROR 20456 --- [io-10087-exec-6] c.h.h.s.c.ReadPictureInfoController      : 标签名: File Modified Date

在Spring Boot项目中保存图片,你可以按照以下步骤进行操作: 1. 在你的Spring Boot项目中创建一个用于保存图片的目录。可以在`resources/static`目录下创建一个名为`images`的文件夹,用来存放图片文件。 2. 创建一个Controller来处理图片上传的请求。你可以使用`@RestController`注解来定义一个Controller,并使用`@PostMapping`注解来处理POST请求。 ```java import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; @RestController public class ImageController { @PostMapping("/upload") public String uploadImage(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { return "上传失败,请选择文件"; } try { // 获取文件名 String fileName = file.getOriginalFilename(); // 指定文件存储路径 String filePath = "src/main/resources/static/images/"; // 创建目标文件对象 File dest = new File(filePath + fileName); // 将上传文件保存到目标文件中 file.transferTo(dest); return "上传成功"; } catch (IOException e) { e.printStackTrace(); return "上传失败"; } } } ``` 3. 在前端页面中添加一个文件上传表单。你可以使用HTML的`<form>`元素以及`<input type="file">`元素来实现文件上传功能。 ```html <form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <button type="submit">上传</button> </form> ``` 请确保表单的`action`属性与Controller中的请求路径一致。 4. 运行你的Spring Boot应用,并访问前端页面。选择一个图片文件并点击上传按钮即可将图片保存到指定目录中。 这样,你就可以在Spring Boot项目中成功保存图片了。请注意,上述代码仅提供了一个简单的示例,你可以根据实际需求进行适当的修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值