我正在使用Drew Noakes的元数据提取器项目。我想将它们存储在.txt文件中,而不是在控制台中打印输出。我该怎么做?



我正在使用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

我想,您需要做这样的事情。

PrintStream out = new PrintStream(new FileOutputStream("E://Project/output.txt"));
System.setOut(out);

这是文件操作如何在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();
         }
      }
   }
}

此代码来自此处

首先,您的文件路径看起来可疑。应该是 "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后,请确保将其关闭,以使其被冲洗并发布任何资源。

我尚未测试此代码,但我认为这将帮助您取得进步。您可能还想自定义输出中标签的显示方式。

相关内容

  • 没有找到相关文章

最新更新