获取图片拍摄时间,剪切移动,复制图片

获取图片信息

引入依赖 metadata-extractor,可以获取图片的详细信息

metadata-extractor是 处理图片EXIF信息的开源项目,最新代码及下载地址:https://github.com/drewnoakes/metadata-extractor

  
      com.drewnoakes
      metadata-extractor
      2.15.0
   

 

获取图片拍摄时间

/**
     * 获取图片的拍摄时间
     *
     * @return void
     * @date 2020-12-8 
     */
    private static Date readPic(File jpegFile) {
        Metadata metadata;
        try {
            metadata = JpegMetadataReader.readMetadata(jpegFile);
            for (Directory directory : metadata.getDirectories()) {
                if ("ExifSubIFDDirectory".equalsIgnoreCase(directory.getClass().getSimpleName())) {
                    return DateUtil.format(directory.getString(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL),"yyyy:MM:dd HH:mm:ss");
                }
            }
        } catch (JpegProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

获取图片信息

/**
     * 获取单张 图片信息
     *
     * @return void
     * @date 2015-7-25 下午7:30:47
     */
    private static void readPic() {
        File jpegFile = new File("C:\\Users\\Administrator\\Desktop\\42.JPG");
        Metadata metadata;
        try {
            metadata = JpegMetadataReader.readMetadata(jpegFile);
            Iterator it = metadata.getDirectories().iterator();
            while (it.hasNext()) {
                Directory exif = it.next();
                Iterator tags = exif.getTags().iterator();
                while (tags.hasNext()) {
                    Tag tag = (Tag) tags.next();
                    System.out.println(tag);
                }
            }
        } catch (JpegProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

输出结果:

[JPEG] Compression Type - Baseline
[JPEG] Data Precision - 8 bits
[JPEG] Image Height - 3000 pixels
[JPEG] Image Width - 4000 pixels
[JPEG] Number of Components - 3
[JPEG] Component 1 - Y component: Quantization table 0, Sampling factors 2 horiz/2 vert
[JPEG] Component 2 - Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert
[JPEG] Component 3 - Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert
[JFIF] Version - 1.1
[JFIF] Resolution Units - none
[JFIF] X Resolution - 1 dot
[JFIF] Y Resolution - 1 dot
[JFIF] Thumbnail Width Pixels - 0
[JFIF] Thumbnail Height Pixels - 0
[Huffman] Number of Tables - 4 Huffman tables
[File] File Name - 42.JPG
[File] File Size - 2160971 bytes
[File] File Modified Date - 星期一 十一月 30 09:47:08 +08:00 2020

 

剪切移动图片

/**
     * 剪切图片
     * @param srcPathStr
     * @param desPathStr
     */
    public static void move(String srcPathStr, String desPathStr) {
        
        try {
            File file = new File(srcPathStr); //源文件
            if (file.renameTo(new File(desPathStr))) //源文件移动至目标文件目录
            {
                System.out.println("File is moved successful!");//输出移动成功
            } else {
                System.out.println("File is failed to move !");//输出移动失败
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

复制粘贴图片

 /**
     * 复制图片
     * @param srcPathStr
     * @param desPathStr
     */
    public static void copy(String srcPathStr, String desPathStr) {
        //获取源文件的名称
        String newFileName = srcPathStr.substring(srcPathStr.lastIndexOf("\\") + 1); //目标文件地址
        System.out.println("源文件:" + newFileName);
        desPathStr = desPathStr + File.separator + newFileName; //源文件地址
        System.out.println("目标文件地址:" + desPathStr);
        try {
            FileInputStream fis = new FileInputStream(srcPathStr);//创建输入流对象
            FileOutputStream fos = new FileOutputStream(desPathStr); //创建输出流对象
            byte datas[] = new byte[1024 * 8];//创建搬运工具
            int len = 0;//创建长度
            while ((len = fis.read(datas)) != -1)//循环读取数据
            {
                fos.write(datas, 0, len);
            }
            fis.close();//释放资源
            fis.close();//释放资源
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

你可能感兴趣的:(java,java)