-1

我正在使用 Drew Noakes 的元数据提取器项目。当我运行 SampleUsage.java 时,它会在控制台中提供输出。我需要将它们存储在 .txt 文件中,当我这样做时,我只会丢失一些信息,例如 ICC、IPTC 信息。我怎样才能存储它们呢?

我的代码在这里:

package com.drew.metadata;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.imaging.jpeg.JpegSegmentMetadataReader;
import com.drew.metadata.exif.ExifReader;
import com.drew.metadata.iptc.IptcReader;
import java.io.BufferedWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;



    public static void main(String[] args)
    {


        File file = new File("Tests/Data/image.jpg");

        try {
            Metadata metadata = ImageMetadataReader.readMetadata(file);

            print(metadata);
        } catch (ImageProcessingException e) {

        } catch (IOException e) {

        }

        try {
            Metadata metadata = JpegMetadataReader.readMetadata(file);

            print(metadata);
        } catch (JpegProcessingException e) {

        } catch (IOException e) {

        }

        try {

            Iterable<JpegSegmentMetadataReader> readers = Arrays.asList(new ExifReader(), new IptcReader());

            Metadata metadata = JpegMetadataReader.readMetadata(file, readers);

            print(metadata);
        } catch (JpegProcessingException e) {

        } catch (IOException e) {

        }
    }

    private static void print(Metadata metadata) throws FileNotFoundException
    {
        PrintStream output = new PrintStream(new File("E:/Project/output.txt"));
         //System.out.println("-------------------------------------");

        for (Directory directory : metadata.getDirectories()) {


            for (Tag tag : directory.getTags()) {

            output.println(tag);
           //System.out.println(tag);

            }

            if (directory.hasErrors()) {
                for (String error : directory.getErrors()) {
                    System.err.println("ERROR: " + error);
                }
            }
        }
    output.close();
    }
}

示例图像: 示例图像

接收输出:

[Exif IFD0] Make - samsung
[Exif IFD0] Model - SM-G360H
[Exif IFD0] X Resolution - 72 dots per inch
[Exif IFD0] Y Resolution - 72 dots per inch
[Exif IFD0] Resolution Unit - Inch
[Exif IFD0] Software - G360HDDU0ANL3
[Exif IFD0] Date/Time - 2016:03:19 14:43:43
[Exif SubIFD] Exposure Time - 1/392 sec
[Exif SubIFD] F-Number - f/2.6
[Exif SubIFD] Exposure Program - Program normal
[Exif SubIFD] ISO Speed Ratings - 50
[Exif SubIFD] Exif Version - 2.20
[Exif SubIFD] Date/Time Original - 2016:03:19 14:43:29
[Exif SubIFD] Date/Time Digitized - 2016:03:19 14:43:29
[Exif SubIFD] Shutter Speed Value - 1/390 sec
[Exif SubIFD] Aperture Value - f/2.6
[Exif SubIFD] Brightness Value - 0
[Exif SubIFD] Exposure Bias Value - 0 EV
[Exif SubIFD] White Balance - Unknown
[Exif SubIFD] Flash - Flash did not fire
[Exif SubIFD] Focal Length - 3.3 mm
[Exif SubIFD] Exposure Mode - Auto exposure
[Exif SubIFD] White Balance Mode - Auto white balance
[Exif SubIFD] Scene Capture Type - Standard
[Exif SubIFD] Contrast - None
[Exif Thumbnail] Compression - JPEG (old-style)
[Exif Thumbnail] X Resolution - 72 dots per inch
[Exif Thumbnail] Y Resolution - 72 dots per inch
[Exif Thumbnail] Resolution Unit - Inch
[Exif Thumbnail] Thumbnail Offset - 589 bytes
[Exif Thumbnail] Thumbnail Length - 15961 bytes
[File] File Name - image.jpg
[File] File Size - 429252 bytes
[File] File Modified Date - Sat Mar 19 17:09:40 +06:00 2016

预期输出:

[JPEG] Compression Type - Baseline
[JPEG] Data Precision - 8 bits
[JPEG] Image Height - 966 pixels
[JPEG] Image Width - 1288 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
[Exif IFD0] Make - samsung
[Exif IFD0] Model - SM-G360H
[Exif IFD0] X Resolution - 72 dots per inch
[Exif IFD0] Y Resolution - 72 dots per inch
[Exif IFD0] Resolution Unit - Inch
[Exif IFD0] Software - G360HDDU0ANL3
[Exif IFD0] Date/Time - 2016:03:19 14:43:43
[Exif SubIFD] Exposure Time - 1/392 sec
[Exif SubIFD] F-Number - f/2.6
[Exif SubIFD] Exposure Program - Program normal
[Exif SubIFD] ISO Speed Ratings - 50
[Exif SubIFD] Exif Version - 2.20
[Exif SubIFD] Date/Time Original - 2016:03:19 14:43:29
[Exif SubIFD] Date/Time Digitized - 2016:03:19 14:43:29
[Exif SubIFD] Shutter Speed Value - 1/390 sec
[Exif SubIFD] Aperture Value - f/2.6
[Exif SubIFD] Brightness Value - 0
[Exif SubIFD] Exposure Bias Value - 0 EV
[Exif SubIFD] White Balance - Unknown
[Exif SubIFD] Flash - Flash did not fire
[Exif SubIFD] Focal Length - 3.3 mm
[Exif SubIFD] Exposure Mode - Auto exposure
[Exif SubIFD] White Balance Mode - Auto white balance
[Exif SubIFD] Scene Capture Type - Standard
[Exif SubIFD] Contrast - None
[Exif Thumbnail] Compression - JPEG (old-style)
[Exif Thumbnail] X Resolution - 72 dots per inch
[Exif Thumbnail] Y Resolution - 72 dots per inch
[Exif Thumbnail] Resolution Unit - Inch
[Exif Thumbnail] Thumbnail Offset - 589 bytes
[Exif Thumbnail] Thumbnail Length - 15961 bytes
[ICC Profile] Profile Size - 3144
[ICC Profile] CMM Type - Lino
[ICC Profile] Version - 2.1.0
[ICC Profile] Class - Display Device
[ICC Profile] Color space - RGB 
[ICC Profile] Profile Connection Space - XYZ 
[ICC Profile] Profile Date/Time - 1998:02:09 06:49:00
[ICC Profile] Signature - acsp
[ICC Profile] Primary Platform - Microsoft Corporation
[ICC Profile] Device manufacturer - IEC 
[ICC Profile] Device model - sRGB
[ICC Profile] XYZ values - 0.964 1 0.825
[ICC Profile] Tag Count - 17
[ICC Profile] Copyright - Copyright (c) 1998 Hewlett-Packard Company
[ICC Profile] Profile Description - sRGB IEC61966-2.1
[ICC Profile] Media White Point - (0.9505, 1, 1.0891)
[ICC Profile] Media Black Point - (0, 0, 0)
[ICC Profile] Red Colorant - (0.4361, 0.2225, 0.0139)
[ICC Profile] Green Colorant - (0.3851, 0.7169, 0.0971)
[ICC Profile] Blue Colorant - (0.1431, 0.0606, 0.7141)
[ICC Profile] Device Mfg Description - IEC http://www.iec.ch
[ICC Profile] Device Model Description - IEC 61966-2.1 Default RGB colour space - sRGB
[ICC Profile] Viewing Conditions Description - Reference Viewing Condition in IEC61966-2.1
[ICC Profile] Viewing Conditions - view (0x76696577): 36 bytes
[ICC Profile] Luminance - (76.0365, 80, 87.1246)
[ICC Profile] Measurement - 1931 2° Observer, Backing (0, 0, 0), Geometry Unknown, Flare 1%, Illuminant D65
[IPTC] Coded Character Set - UTF-8
[IPTC] Enveloped Record Version - 4
[IPTC] Caption/Abstract - 
[IPTC] Keywords - about cse#01;<name>      " emam";<friendof>    "sujon";<place>        "kotbar"i;<about>         "sujon";<friendof>      "emam";<description>   "cvc"
[File] File Name - image.jpg
[File] File Size - 429252 bytes
[File] File Modified Date - Sat Mar 19 17:09:40 +06:00 2016
4

3 回答 3

0

首先,您的文件路径看起来很可疑。应该是"E:/Project/output.txt",冒号后只有一个正斜杠吗?

其次,您正在为每个标签创建一个打印流。您应该在循环之外创建它。

public static void saveMetadataToFile(File image, File output)
    throws ImageProcessingException, IOException
{
    Metadata metadata = ImageMetadataReader.readMetadata(image);
    PrintStream printStream = new PrintStream(output);
    try {
        for (Directory directory : metadata.getDirectories()) {
            for (Tag tag : directory.getTags()) {
                printStream.println(tag);
            }
        }
    } finally {
        printStream.close();
    }
}

完成后,请PrintStream务必关闭它,以便刷新它并释放任何资源。

我没有测试过这段代码,但我认为它会帮助你取得进步。您可能还想自定义标签在输出中的显示方式。

于 2017-04-15T13:20:17.097 回答
0

我认为,你需要做这样的事情。

PrintStream out = new PrintStream(new FileOutputStream("E://Project/output.txt"));
System.setOut(out);
于 2017-04-15T06:42:16.903 回答
0

这是一个文件操作如何在 Java 中工作的示例。

import java.io.*;
public class CopyFile {

   public static void main(String args[]) throws IOException {  
      FileInputStream in = null;
      FileOutputStream out = null;

      try {
         in = new FileInputStream("input.txt");
         out = new FileOutputStream("output.txt");

         int c;
         while ((c = in.read()) != -1) {
            out.write(c);
         }
      }finally {
         if (in != null) {
            in.close();
         }
         if (out != null) {
            out.close();
         }
      }
   }
}

这段代码来自这里

于 2017-04-15T06:45:14.760 回答