Я использую проект экстрактора метаданных Дрю Ноакса. Вместо того, чтобы печатать вывод в консоли, я хочу сохранить их в файле.txt. Как я могу это сделать?
Я использую проект экстрактора метаданных Дрю Ноакса. Когда я запускаю SampleUsage.java, он выводит данные в консоли. Мне нужно хранить их в текстовом файле. Когда я это делаю, я просто теряю некоторую информацию, такую как 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
3 ответа
Вот пример того, как работает манипулирование файлами в 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();
}
}
}
}
Этот код отсюда
Я думаю, тебе нужно сделать что-то подобное.
PrintStream out = new PrintStream(new FileOutputStream("E://Project/output.txt"));
System.setOut(out);
Во-первых, путь к вашему файлу выглядит подозрительно. Должно ли это быть "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
не забудьте закрыть его, чтобы он был сброшен и все ресурсы были освобождены.
Я не тестировал этот код, но думаю, что он поможет вам добиться прогресса. Вы также можете настроить отображение тега в выходных данных.