Olá comunidade, boa tarde.
Estou testado o apache tika para ler metadados de arquivos de imagens, som, texto e por ai vai.
Porém, não estou achando como faço para salvar (gravar) metadados no meu arquivo.
Tenho uma classe de exemplo…
package ses.metadados.exemplos;
import java.io.*;
// Apache Tika Classes.
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
// end apache tika
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
public class UsingApacheTika{
public static void main(String[] arguments) throws IOException{
/**
* Arquivo original vindo do site @{http://www.rgagnon.com/javadetails/java-0487.html}
*
*
FileInputStream fileInput = null;
try{
File file = new File("/home/paulopatto/Documentos/ses/reuniao.docx");
fileInput = new FileInputStream( file );
// from Apache Tika
ContentHandler contentHandler = new BodyContentHandler();
Metadata metadata = new Metadata();
metadata.set( Metadata.RESOURCE_NAME_KEY, file.getName() );
Parser p = new AutoDetectParser();
p.parse(fileInput, contentHandler, metadata);
System.out.println("Mime type: " + metadata.get(Metadata.CONTENT_TYPE));
System.out.println("Título: " + metadata.get(Metadata.TITLE));
System.out.println("Autor: " + metadata.get(Metadata.AUTHOR));
System.out.println("-----------------------------------------------");
System.out.println("Conteúdo: " + contentHandler.toString());
System.out.println("-----------------------------------------------");
System.out.println("Palavras Chaves: " + metadata.get(Metadata.KEYWORDS));
}
catch(Exception e ){e.printStackTrace();}
finally{if(fileInput != null) fileInput.close();}
*
*/
UsingApacheTika app = new UsingApacheTika();
/// Substitua os arquivos por um docx, jpg, doc, png, mp3 e por ai vai...
app.exibir("/home/paulopatto/Documentos/ses/reuniao.docx");
System.out.println("\n\n");
app.exibir("/home/paulopatto/Documentos/ses/definicao_processos.doc");
System.out.println("\n\n");
app.exibir("/home/paulopatto/Documentos/ses/logos/logo_ses.jpg");
System.out.println("\n\n");
app.exibir("/home/paulopatto/Imagens/icons/rss.png");
System.out.println("\n\n");
app.exibir("/home/paulopatto/Imagens/shotterstock/shutterstock_32533963.jpg");
System.out.println("\n\n");
app.exibir("/home/paulopatto/Imagens/shotterstock/shutterstock_34446118.jpg");
System.out.println("\n\n");
System.out.println("Escrevendo...\n\n");
app.setar(Metadata.COMPANY,"ses","/home/paulopatto/Imagens/shotterstock/shutterstock_34446118.jpg");
app.setar(Metadata.AUTHOR,"SHOTTER STOCK","/home/paulopatto/Imagens/shotterstock/shutterstock_34446118.jpg");
app.setar(Metadata.RIGHTS,"ses / Shotter Stock","/home/paulopatto/Imagens/shotterstock/shutterstock_34446118.jpg");
app.setar(Metadata.KEYWORDS,"Mulher, luta, boxe, esporte","/home/paulopatto/Imagens/shotterstock/shutterstock_34446118.jpg");
app.setar(Metadata.DATE,"2010-08-31 17:42","/home/paulopatto/Imagens/shotterstock/shutterstock_34446118.jpg");
app.exibir("/home/paulopatto/Imagens/shotterstock/shutterstock_34446118.jpg");
System.out.println("\n\n");
// shutterstock_34446118
}
/**
* Exibe os metadados de um arquivo de acordo com a url passada.
* @param url:string
*/
protected void exibir( String url ) throws IOException{
FileInputStream file = null;
try{
File f = new File (url);
file = new FileInputStream(f);
ContentHandler contentHandler = new BodyContentHandler();
Metadata metadata = new Metadata();
metadata.set( Metadata.RESOURCE_NAME_KEY, f.getName() );
Parser p = new AutoDetectParser();
p.parse(file, contentHandler, metadata);
System.out.println("-----------------------------------------------");
System.out.println("Mime type: " + metadata.get(Metadata.CONTENT_TYPE));
System.out.println("Título: " + metadata.get(Metadata.TITLE));
System.out.println("Autor: " + metadata.get(Metadata.AUTHOR));
System.out.println("-----------------------------------------------");
if(contentHandler.toString().length() > 0)
System.out.println("Conteúdo: " + contentHandler.toString().substring(0, 137) + "...");
System.out.println("-----------------------------------------------");
System.out.println("Companhia: " + metadata.get(Metadata.COMPANY));
System.out.println("compyright: " + metadata.get(Metadata.RIGHTS));
System.out.println("-----------------------------------------------");
System.out.println("Palavras Chaves: " + metadata.get(Metadata.KEYWORDS));
System.out.println("-----------------------------------------------");
}
catch(IOException e){
System.err.println("Erro com I/O: Verifique se a url passada é correta.");
System.err.println("Erro: " + e.getMessage());
}
catch(SAXException e){
System.err.println("Erro com SAX: Ocorreu um erro com o parse do SAX, verifique o trace do erro.");
System.err.println("Erro: " + e.getMessage());
}
catch(TikaException e){
System.err.println("Erro com TIKA: Ocorreu um erro com o parse do Apache Tika, verifique o trace do erro.");
System.err.println("Erro: " + e.getMessage());
}
catch(Exception e){
System.err.println("Erro desconhecido: Ocorreu um erro desconhecido, verifique o trace do erro.");
System.err.println("Erro: " + e.getMessage());
}
finally{
if ( file != null ) {file.close();}
}
}
protected void setar (String property, String value, String url) throws IOException{
FileInputStream file = null;
try{
File f = new File (url);
file = new FileInputStream(f);
ContentHandler contentHandler = new BodyContentHandler();
Metadata metadata = new Metadata();
metadata.set( Metadata.RESOURCE_NAME_KEY, f.getName() );
metadata.set(property, value);
Parser p = new AutoDetectParser();
p.parse(file, contentHandler, metadata);
System.err.println(metadata.toString());
}
catch(IOException e){
System.err.println("Erro com I/O: Verifique se a url passada é correta.");
System.err.println("Erro: " + e.getMessage());
}
catch(SAXException e){
System.err.println("Erro com SAX: Ocorreu um erro com o parse do SAX, verifique o trace do erro.");
System.err.println("Erro: " + e.getMessage());
}
catch(TikaException e){
System.err.println("Erro com TIKA: Ocorreu um erro com o parse do Apache Tika, verifique o trace do erro.");
System.err.println("Erro: " + e.getMessage());
}
catch(Exception e){
System.err.println("Erro desconhecido: Ocorreu um erro desconhecido, verifique o trace do erro.");
System.err.println("Erro: " + e.getMessage());
}
finally{
if ( file != null ) {file.close();}
}
}
}
Ok ler as informações eu consigo, mas escrever…
Segue aqui as minhas referencias e enquanto isso vou testando também o JMagick com ImageMagick.
[list] http://tika.apache.org/0.7/api/[/list]
[list] http://tika.apache.org/index.html[/list]
[list] http://www.rgagnon.com/javadetails/java-0487.html[/list]
[list] http://www.barregren.se/blog/how-read-exif-and-iptc-java-image-i-o-api[/list]
[list] http://www.drewnoakes.com/code/exif/[/list]
[list] Não adicionaei todo o pache tika pois mesmo com uma compactação BZip2 ele fica por completo com 53MB. É necessário tem o maven montar o projeto tika.[/list]