I have been trying to access modified metadata of images in Java for the past few hours. I know I am one or two steps away from getting the correct output. Would really appreciate if someone can help me with this.
I want to add an extra field in the metadata, which is like an text similar to:
Writing image metadata in Java, preferably PNG
My issue is that when i add the custom data to my image and when i read the image, i do not see the change in the modified metadata when i call the method readAndDisplay. I think the reason is that i am not saving the image properly into a new file with the modified metadata. Can someone have a look at what i am missing from my code:
public class Metadata {
public static void main(String[] args) throws Exception {
String imageFile = "134.png";
BufferedImage img = null;
try {
img = ImageIO.read(new File(imageFile));
} catch (IOException e) {
System.out.println(e.getMessage());
}
Metadata meta = new Metadata();
byte[] result = meta.writeCustomData(img, "decimalID", "211");
BufferedImage output = ImageIO.read(new ByteArrayInputStream(result));
ImageIO.write(output, "png", new File("output.png"));
meta.readAndDisplayMetadata("output.png");
}
void readAndDisplayMetadata( String fileName ) {
try {
File file = new File( fileName );
ImageInputStream iis = ImageIO.createImageInputStream(file);
Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
if (readers.hasNext()) {
// pick the first available ImageReader
ImageReader reader = readers.next();
// attach source to the reader
reader.setInput(iis, true);
// read metadata of first image
IIOMetadata metadata = reader.getImageMetadata(0);
String[] names = metadata.getMetadataFormatNames();
int length = names.length;
System.out.println(length);
for (int i = 0; i < length; i++) {
System.out.println( "Format name: " + names[ i ] );
displayMetadata(metadata.getAsTree(names[i]));
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
void readAndDisplayMetadata( byte[] image ) {
try {
// File file = new File( fileName );
ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(image));
Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
if (readers.hasNext()) {
// pick the first available ImageReader
ImageReader reader = readers.next();
// attach source to the reader
reader.setInput(iis, true);
// read metadata of first image
IIOMetadata metadata = reader.getImageMetadata(0);
String[] names = metadata.getMetadataFormatNames();
int length = names.length;
System.out.println(length);
for (int i = 0; i < length; i++) {
System.out.println( "Format name: " + names[ i ] );
displayMetadata(metadata.getAsTree(names[i]));
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
void displayMetadata(Node root) {
displayMetadata(root, 0);
}
void indent(int level) {
for (int i = 0; i < level; i++)
System.out.print(" ");
}
void displayMetadata(Node node, int level) {
// print open tag of element
indent(level);
//System.out.println("Attributes: " + node.getAttributes().getLength());
System.out.print("<" + node.getNodeName());
NamedNodeMap map = node.getAttributes();
if (map != null) {
// print attribute values
int length = map.getLength();
for (int i = 0; i < length; i++) {
Node attr = map.item(i);
System.out.print(" " + attr.getNodeName() +
"=\"" + attr.getNodeValue() + "\"");
}
}
Node child = node.getFirstChild();
if (child == null) {
// no children, so close element and return
System.out.println("/>");
return;
}
// children, so close current tag
System.out.println(">");
while (child != null) {
// print children recursively
displayMetadata(child, level + 1);
child = child.getNextSibling();
}
// print close tag of element
indent(level);
System.out.println("</" + node.getNodeName() + ">");
}
public byte[] writeCustomData(BufferedImage buffImg, String key, String value) throws Exception {
ImageWriter writer = ImageIO.getImageWritersByFormatName("png").next();
ImageWriteParam writeParam = writer.getDefaultWriteParam();
ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
//adding metadata
IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
IIOMetadataNode textEntry = new IIOMetadataNode("tEXtEntry");
textEntry.setAttribute("keyword", key);
textEntry.setAttribute("value", value);
IIOMetadataNode text = new IIOMetadataNode("tEXt");
text.appendChild(textEntry);
IIOMetadataNode root = new IIOMetadataNode("javax_imageio_png_1.0");
root.appendChild(text);
metadata.mergeTree("javax_imageio_png_1.0", root);
//writing the data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream stream = ImageIO.createImageOutputStream(baos);
writer.setOutput(stream);
writer.write(metadata, new IIOImage(buffImg, null, metadata), writeParam);
stream.close();
return baos.toByteArray();
}
Related
Anyone knows how to detect uris from moved files programmatically in Java/Android?When I start the app I detect all images in my phone (with their uris, path....), if I move the images with a media manager, next time I start the app I can get the whole uris again, with the new path. But, if I move these images programmatically (copying the image and deleting the image from the original path) next time I start the app the uris will be the previous path a not the new one. I'm trying to fix deleting the app cache, but doesn't work. Anyone knows what can be happen?
I tried to move the files with two functions and I have same problem:
public static void moveFile(ArrayList<ImagesData> images) throws IOException {
for (int i = 0; i < images.size(); i++) {
File file_Source = new File(images.get(i).imagePath);
File file_Destination = new File(Environment.getExternalStorageDirectory() + "/PrivateGallery/" + new File(images.get(i).imagePath).getName());
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(file_Source).getChannel();
destination = new FileOutputStream(file_Destination).getChannel();
Log.i(TAG, "Source " + source + " destination " + destination);
long count = 0;
long size = source.size();
while((count += destination.transferFrom(source, count, size-count)) < size);
//destination.transferFrom(source, 0, source.size());
file_Source.delete();
}
finally {
if(source != null) {
source.close();
}
if(destination != null) {
destination.close();
}
}
}
}
public void changeToNewPath(ArrayList<ImagesData> images) {
String outputPath = Environment.getExternalStorageDirectory() + "/PrivateGallery/";
//TODO COMPROBAR SI EXISTE YA UN ARCHIVO CON SU NOMBRE
InputStream in = null;
OutputStream out = null;
for(int i = 0; i < images.size(); i++) {
try { //TODO revisar lod el cambio de directorio
//create output directory if it doesn't exist
//File dir = new File(outputPath);
File f = new File(images.get(i).imagePath);
String a = f.getName();
Log.e(TAG, a);
in = new FileInputStream(images.get(i).imagePath);
out = new FileOutputStream(outputPath + new File(images.get(i).imagePath).getName());
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
// write the output file
out.flush();
out.close();
out = null;
// delete the original file
new File(images.get(i).imagePath).delete();
} catch (FileNotFoundException fnfe1) {
Log.e("tag", fnfe1.getMessage());
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
}
Hello I was able to convert a tif file to jpeg with the following code that I got from
https://stackoverflow.com/questions/15429011/how-to-convert-tiff-to-jpeg-png-in-java#=
String inPath = "./tifTest/113873996.002.tif";
String otPath = "./tifTest/113873996.002-0.jpeg";
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new FileInputStream(inPath), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(new FileOutputStream(otPath), DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(TifToJpeg.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(TifToJpeg.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
This only works with one-page tif file, and when I use it with a multi-page tif, it only saves the first page.
How can I modified this to save a mymultipagetif.tif into:
mymultipagetif-0.jpeg
mymultipagetif-1.jpeg
mymultipagetif-2.jpeg
Thanks!
This will takes a multi-page TIFF file (in the SeekableStream), extract the pages in the pages array ("1", "3", "4" for example) and write them into a single multi-page tiff into the file outTIffFileName. Modify as desired.
private String _ExtractListOfPages (SeekableStream ss, String outTiffFileName, String[] pages){
// pageNums is a String array of 0-based page numbers.
try {
TIFFDirectory td = new TIFFDirectory(ss, 0);
if (debugOn) {
System.out.println("Directory has " + Integer.toString(td.getNumEntries()) + " entries");
System.out.println("Getting TIFFFields");
System.out.println("X resolution = " + Float.toString(td.getFieldAsFloat(TIFFImageDecoder.TIFF_X_RESOLUTION)));
System.out.println("Y resolution = " + Float.toString(td.getFieldAsFloat(TIFFImageDecoder.TIFF_Y_RESOLUTION)));
System.out.println("Resolution unit = " + Long.toString(td.getFieldAsLong(TIFFImageDecoder.TIFF_RESOLUTION_UNIT)));
}
ImageDecoder decodedImage = ImageCodec.createImageDecoder("tiff", ss, null);
int count = decodedImage.getNumPages();
if (debugOn) { System.out.println("Input image has " + count + " page(s)"); }
TIFFEncodeParam param = new TIFFEncodeParam();
TIFFField tf = td.getField(259); // Compression as specified in the input file
param.setCompression(tf.getAsInt(0)); // Set the compression of the output to be the same.
param.setLittleEndian(false); // Intel
param.setExtraFields(td.getFields());
FileOutputStream fOut = new FileOutputStream(outTiffFileName);
Vector<RenderedImage> vector = new Vector<RenderedImage>();
RenderedImage page0 = decodedImage.decodeAsRenderedImage(Integer.parseInt(pages[0]));
BufferedImage img0 = new BufferedImage(page0.getColorModel(), (WritableRaster)page0.getData(), false, null);
int pgNum;
// Adding the extra pages starts with the second one on the list.
for (int i = 1; i < pages.length; i++ ) {
pgNum = Integer.parseInt(pages[i]);
if (debugOn) { System.out.println ("Page number " + pgNum); }
RenderedImage page = decodedImage.decodeAsRenderedImage(pgNum);
if (debugOn) { System.out.println ("Page is " + Integer.toString(page.getWidth()) + " pixels wide and "+ Integer.toString(page.getHeight()) + " pixels high."); }
if (debugOn) { System.out.println("Adding page " + pages[i] + " to vector"); }
vector.add(page);
}
param.setExtraImages(vector.iterator());
ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", fOut, param);
if (debugOn) { System.out.println("Encoding page " + pages[0]); }
encoder.encode(decodedImage.decodeAsRenderedImage(Integer.parseInt(pages[0])));
fOut.close();
} catch (Exception e) {
System.out.println(e.toString());
return("Not OK " + e.getMessage());
}
return ("OK");
}
hello so I have been writing an updater for my game.
1) It checks a .version file on drop box and compares it to the local .version file.
2) If there is any link missing from the local version of the file, it downloads the required link one by one.
The issue I am having is some of the users can download the zips and some cannot.
One of my users who was having the issue was using windows xp. So some of them have old computers.
I was wondering if anyone could help me to get an idea on what could be causing this.
This is the main method that is ran
public void UpdateStart() {
System.out.println("Starting Updater..");
if(new File(cache_dir).exists() == false) {
System.out.print("Creating cache dir.. ");
while(new File(cache_dir).mkdir() == false);
System.out.println("Done");
}
try {
version_live = new Version(new URL(version_file_live));
} catch(MalformedURLException e) {
e.printStackTrace();
}
version_local = new Version(new File(version_file_local));
Version updates = version_live.differences(version_local);
System.out.println("Updated");
int i = 1;
try {
byte[] b = null, data = null;
FileOutputStream fos = null;
BufferedWriter bw = null;
for(String s : updates.files) {
if(s.equals(""))
continue;
System.out.println("Reading file "+s);
text = "Downloading file "+ i + " of "+updates.files.size();
b = readFile(new URL(s));
progress_a = 0;
progress_b = b.length;
text = "Unzipping file "+ i++ +" of "+updates.files.size();
ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(b));
File f = null, parent = null;
ZipEntry entry = null;
int read = 0, entry_read = 0;
long entry_size = 0;
progress_b = 0;
while((entry = zipStream.getNextEntry()) != null)
progress_b += entry.getSize();
zipStream = new ZipInputStream(new ByteArrayInputStream(b));
while((entry = zipStream.getNextEntry()) != null) {
f = new File(cache_dir+entry.getName());
if(entry.isDirectory())
continue;
System.out.println("Making file "+f.toString());
parent = f.getParentFile();
if(parent != null && !parent.exists()) {
System.out.println("Trying to create directory "+parent.getAbsolutePath());
while(parent.mkdirs() == false);
}
entry_read = 0;
entry_size = entry.getSize();
data = new byte[1024];
fos = new FileOutputStream(f);
while(entry_read < entry_size) {
read = zipStream.read(data, 0, (int)Math.min(1024, entry_size-entry_read));
entry_read += read;
progress_a += read;
fos.write(data, 0, read);
}
fos.close();
}
bw = new BufferedWriter(new FileWriter(new File(version_file_local), true));
bw.write(s);
bw.newLine();
bw.close();
}
} catch(Exception e) {
this.e = e;
e.printStackTrace();
return;
}
System.out.println(version_live);
System.out.println(version_local);
System.out.println(updates);
try {
} catch (Exception er) {
er.printStackTrace();
}
}
I have been trying to fix this for the last two days and I am just so stumped at this point
All the best,
Christian
public class ExtractText
{
/**
* private constructor.
*/
private ExtractText()
{
//static class
}
public static void main( String[] args ) throws Exception
{
if(l!=null)
{
System.out.println("HERE"+l.length);
deleteSubs(op);
System.out.println("Then"+l.length);
}
else
{
System.out.println("WHERE");
}
File y=new File(imgDes);
if(!y.exists())
{
y.mkdirs();
}
File z=new File(imgDestination);
if(!z.exists())
{
z.mkdirs();
}
File fr=new File(outputFile);
if(!fr.isDirectory())
{
fr.delete();
}
// Defaults to text files
String ext = ".txt";
int startPage = 1;
int endPage = Integer.MAX_VALUE;
Writer output = null;
PDDocument document =null;
try
{
try
{
URL url = new URL( pdfFile );
document = PDDocument.load(url, force);
String fileName = url.getFile();
if( outputFile == null && fileName.length() >4)
{
outputFile = new File( fileName.substring( 0, fileName.length() -4 ) + ext ).getName();
}
}
catch( MalformedURLException e)
{
document = PDDocument.load(pdfFile, force);
if( outputFile == null && pdfFile.length() >4 )
{
outputFile = pdfFile.substring( 0, pdfFile.length() -4 ) + ext;
}
}
//document.print();
if( document.isEncrypted() )
{
StandardDecryptionMaterial sdm = new StandardDecryptionMaterial( password );
document.openProtection(sdm);
AccessPermission ap = document.getCurrentAccessPermission();
if( ! ap.canExtractContent() )
{
throw new IOException("You do not have permission to extract text" );
}
}
if ((encoding == null) && (toHTML))
{
encoding = "UTF-8";
}
if( toConsole )
{
output = new OutputStreamWriter(System.out);
}
else
{
if( encoding != null )
{
output = new OutputStreamWriter(new FileOutputStream( outputFile ), encoding );
}
else
{
//use default encoding
output = new OutputStreamWriter(new FileOutputStream( outputFile ) );
}
}
PDFTextStripper4 stripper = null;
if(toHTML)
{
stripper = new PDFText2HTML(encoding);
}
else
{
stripper = new PDFTextStripper4(encoding);
}
File f= new File(imgDestination);
PDDocument pd;
int i=0;
if(f.exists())
{
pd=PDDocument.load(pdfFile);
PDFontDescriptor fd;
fd = new PDFontDescriptorDictionary();
List<PDPage> li=pd.getDocumentCatalog().getAllPages();
for(PDPage page:li)
{
PDResources pdr=page.getResources();
Map<String, PDFont> m=pdr.getFonts();
PDStream pst;
for(PDFont pdd:m.values())
{
System.out.println("----------"+pdd.getBaseFont());
pdd.getFontDescriptor();
fd = pdd.getFontDescriptor();
pdd.setFontDescriptor((PDFontDescriptorDictionary)fd);
System.out.println("tititititi"+pdd.getFontEncoding());
if(pdd.isType1Font())
{
pst=((PDFontDescriptorDictionary) fd).getFontFile3();
System.out.println("In If "+pst);
if(pst!= null)
{
FileOutputStream fos = new FileOutputStream(new File(imgDestination+pdd.getBaseFont().toString()+".pfb"));
IOUtils.copy(pst.createInputStream(), fos);
i++;
System.out.println(i);
fos.close();
}
}
else
if(pdd.isTrueTypeFont())
{
pst= ((PDFontDescriptorDictionary) fd).getFontFile2();
System.out.println("In Else-if"+pst);
if (pst!= null)
{
FileOutputStream fos = new FileOutputStream(new File(imgDestination+pdd.getBaseFont().toString()+".ttf"));
IOUtils.copy(pst.createInputStream(), fos);
i++;
System.out.println(i);
fos.close();
}
}
else
if(pdd.isSymbolicFont())
{
System.out.println("Symbol.......");
}
else
{
System.out.println("In Else");
}
}
}
int pageCount = document.getDocumentCatalog().getAllPages().size();
for (int p = 0; p < pageCount; ++p)
{
System.out.println("I am in for loop");
stripper.setForceParsing( force );
stripper.setSortByPosition( true );
stripper.setShouldSeparateByBeads(separateBeads);
stripper.setStartPage( p);
stripper.setEndPage( p);
stripper.writeText( document, output );
FileOutputStream fos = new FileOutputStream(new File(f5+(p+1)+".html"));
output.close();
}
PDDocumentInformation info = document.getDocumentInformation();
System.out.println( "Page Count=" + document.getNumberOfPages());
System.out.println( "Title=" + info.getTitle());
System.out.println( "Author=" + info.getAuthor());
System.out.println( "Subject=" + info.getSubject() );
System.out.println( "Keywords=" + info.getKeywords() );
System.out.println( "Creator=" + info.getCreator() );
System.out.println( "Producer=" + info.getProducer() );
System.out.println( "Creation Date=" + info.getCreationDate() );
System.out.println( "Modification Date=" + info.getModificationDate());
System.out.println( "Trapped=" + info.getTrapped());
}
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
if( output != null)
{
output.close();
}
if( document != null )
{
document.close();
}
}
}
private static void deleteSubs(File op)
{
// TODO Auto-generated method stub
File[] files = op.listFiles();
System.out.print("In delete folder");
if(files!=null)
{
//some JVMs return null for empty dirs
for(File f: files)
{
if(f.isDirectory())
{
deleteSubs(f);
}
else
{
f.delete();
}
}
}
op.delete();
}
}
now i am able to get entire pdf to a html file i.e.. I am extracting text only not images but i want to get every page of a pdf in to single html so any solution for this is quite helpful to me.. ThankYou
The answer is in your question: just set
stripper.setStartPage( p );
stripper.setEndPage( p );
accordingly. So you would loop somewhat like this:
int pageCount = document.getDocumentCatalog().getAllPages().size();
for (int p = 0; p < pageCount; ++p)
{
//... your options
stripper.setStartPage(p);
stripper.setEndPage(p);
FileOutputStream fos = new FileOutputStream(new File(f5+(p+1)+".html"));
stripper.writeText(document, fos);
fos.close();
}
Btw if you get an exception relating to the sorting comparator, use setSortByPosition(false), or wait for version 1.8.8 where this problem is fixed.
am using Docx4j to generate a word document in a swing application.I want to add a picture to the header. The document is created successfully but the picture is not displayed. Below is the code snippets of the applications. I am using the docx4j-nightly-20141016.jar file.
import org.docx4j.wml.ObjectFactory;
public class WordDoc {
private WordprocessingMLPackage wordMLPackage;
private ObjectFactory factory;
private Hdr header;
public WordDoc() {
}
public void createWordDoc() throws Docx4JException, IOException, Exception {
wordMLPackage = WordprocessingMLPackage.createPackage();
factory = Context.getWmlObjectFactory();
Relationship relationship = createHeaderPart();
createHeaderReference(relationship);
wordMLPackage.getMainDocumentPart().addParagraphOfText("Hello Word!");
File file = new File("src/resources/images/logo.jpg");
byte[] bytes = convertImageToByteArray(file);
addImageInline(bytes);
wordMLPackage.save(new java.io.File("src/files/HelloWord14.docx"));
}
private Relationship createHeaderPart() throws InvalidFormatException {
HeaderPart headerPart = new HeaderPart();
headerPart.setPackage(wordMLPackage);
headerPart.setJaxbElement(createHeader("Text"));
return wordMLPackage.getMainDocumentPart().addTargetPart(headerPart);
}
private Hdr createHeader(String content) {
header = factory.createHdr();
P paragraph = factory.createP();
R run = factory.createR();
Text text = new Text();
text.setValue(content);
run.getContent().add(text);
paragraph.getContent().add(run);
header.getContent().add(paragraph);
return header;
}
private void createHeaderReference(Relationship relationship) {
List<SectionWrapper> sections
= wordMLPackage.getDocumentModel().getSections();
SectPr sectionProperties = sections.get(sections.size() - 1).getSectPr();
// There is always a section wrapper, but it might not contain a sectPr
if (sectionProperties == null) {
sectionProperties = factory.createSectPr();
wordMLPackage.getMainDocumentPart().addObject(sectionProperties);
sections.get(0).setSectPr(sectionProperties);
}
HeaderReference headerReference = factory.createHeaderReference();
headerReference.setId(relationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectionProperties.getEGHdrFtrReferences().add(headerReference);
}
private byte[] convertImageToByteArray(File file)
throws FileNotFoundException, IOException {
InputStream is = new FileInputStream(file);
long length = file.length();
// You cannot create an array using a long, it needs to be an int.
if (length > Integer.MAX_VALUE) {
System.out.println("File too large!!");
}
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length -
offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read
if (offset < bytes.length) {
System.out.println("Could not completely read file "
+ file.getName());
}
is.close();
return bytes;
}
private void addImageInline(byte[] bytes) throws Exception {
BinaryPartAbstractImage imagePart
= BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
int docPrId = 1;
int cNvPrId = 2;
Inline inLine = imagePart.createImageInline("Filename hint",
"Alternative text", docPrId, cNvPrId, false);
if (header != null) {
addInlineImageToHeader(inLine);
}
}
private void addInlineImageToHeader(Inline inline) {
// Now add the in-line image to a paragraph
ObjectFactory factory2 = new ObjectFactory();
P paragraph2 = factory2.createP();
R run = factory.createR();
paragraph2.getContent().add(run);
Drawing drawing = factory.createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
header.getContent().add(paragraph2);
}
}
the screen capture of the generated word document is displayed below
Will be very glad to take any suggestions.
Instead of:
BinaryPartAbstractImage imagePart
= BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
you need to add the image part as a rel of your header part, so use:
public static BinaryPartAbstractImage createImagePart(
OpcPackage opcPackage,
Part sourcePart, byte[] bytes) throws Exception
passing it headerPart