Raarm
Novembro 26, 2007, 9:46am
#1
Eai galera…estou com um problema! Eu estou usando um framework para captura de imagem via scanner (MORENA). Na hora de salvar estou usando a classe JFileChooser. Mas eu quero que a imagem seja salva diretamente em uma pasta com nome e tipo (JPG, PNG, TIFF…) previamente setados.
private class SaveImageAction implements Runnable
{ private class Filter extends FileFilter
{ String type;
Filter(String type)
{ this.type=type.toUpperCase();
}
public boolean accept(File file)
{ String name=file.getName().toUpperCase();
return name.endsWith(type);
}
public String getDescription()
{ return type+" Files";
}
}
public synchronized void run()
{ try
{
Image image=selected.getImage();
BufferedImage bufferedImage=new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
bufferedImage.createGraphics().drawImage(image, 0, 0, null);
JFileChooser chooser=new JFileChooser();
String e[]=ImageCodec.getEncoderNames(bufferedImage, null);
for (int i=0; i<e.length; i++)
chooser.addChoosableFileFilter(new Filter(e[i]));
int result=chooser.showSaveDialog(MorenaStudio.this);
if (result==JFileChooser.APPROVE_OPTION)
{ String ext=chooser.getFileFilter().getDescription();
ext=ext.substring(0, ext.indexOf(' ')).toLowerCase();
File file=chooser.getSelectedFile();
String name=file.getName();
if (!name.endsWith(ext))
file=new File(file.getParentFile(), name+"."+ext);
OutputStream tmp=new FileOutputStream(file);
ImageCodec.createImageEncoder(ext, tmp, null).encode(bufferedImage);
tmp.close();
JOptionPane.showMessageDialog(null, "Arquivo Salvo com Sucesso!!","Arquivo",JOptionPane.INFORMATION_MESSAGE);
}
}
catch (Throwable exception)
{ JOptionPane.showMessageDialog(MorenaStudio.this, exception.toString(), "Error", JOptionPane.ERROR_MESSAGE);
exception.printStackTrace();
System.err.println("Erro, tente de novo! ..."+ exception);
}
}
}
Do jeito que esta no codigo, ele me abre uma janela onde eu escolho tipo e a pasta onde quero salvar…estou querendo evitar este passo e salvar direto!
Alguem tem alguma idéia ?
:?
VLW >
A classe “File” não te serve?
File arquivo = new File("diretorio/nomeDoArquivo.extensao")
Raarm
Novembro 26, 2007, 11:05am
#3
AH legal Cintia. deu certo!!!
String ext="png";
String name = "\imagem";
File file=new File(name+"."+ext);
OutputStream tmp=new FileOutputStream(file);
ImageCodec.createImageEncoder("png", tmp, null).encode(bufferedImage);
tmp.close();
Raarm
Novembro 26, 2007, 11:16am
#4
Cintia,
Sera que daria para mandar este arquivo via FTP ?
Eu nunca tentei usar FTP com java, mas eu pesquisei por [google]ftp java[/google] e cai aqui ó
http://www.guj.com.br/posts/list/17103.java
Raarm
Novembro 26, 2007, 11:46am
#6
Nossa…Era isso mesmo que eu queria fazer…vou dar uma implementada no meu codigo para usar FTP!! Vlw…Cintia!!!
Raarm
Novembro 26, 2007, 2:30pm
#7
O problema agora é como ir salvando arquivos com nomes iguais…sei lá colocando algo no final como nomearquivo(1).
File file=new File(path+name+"."+ext);
if(file.exists())
{
OutputStream tmp=new FileOutputStream(path+name+"(1)."+ext);
ImageCodec.createImageEncoder(ext, tmp, null).encode(bufferedImage);
tmp.close();
JOptionPane.showMessageDialog(null, "Arquivo Salvo com Sucesso!!","Arquivo",JOptionPane.INFORMATION_MESSAGE);
}
else
{
OutputStream tmp=new FileOutputStream(file);
ImageCodec.createImageEncoder(ext, tmp, null).encode(bufferedImage);
tmp.close();
JOptionPane.showMessageDialog(null, "Arquivo Salvo com Sucesso!!","Arquivo",JOptionPane.INFORMATION_MESSAGE);
}
O problema é se o arquivo aparecer novamente ele sobrescreve o nomearquivo(1)…eu precisaria que ele fizesse nomearquivo(1)(1).
Alguma sugestao ?
faz um try
e se existir este arquivo ele faz um new file.
ou daki a pouco terás um arrayList de inteiros para cada criação ele debitar um numero diferente!!!
arquivo(1);arquivo (2); arquivo(3)
Raarm
Novembro 26, 2007, 6:18pm
#9
Nao entendi…como seria este try-catch ?
boolean salvo = false;
int i = 1;
while ( salvo == false){
try{
salvarArquivo(nomeDoArquivo+"("+i+")");
salvo = true;
} catch(FileException e){ // eu não sei qual exatamente é a exception que vc tem que tratar
i++;
}
}
Só cuidado para não entrar em loop caso a excessão seja outra (sei lá, acesso a disco, etc etc). Trate as demais excessões, ou coloque um limite no número de tentativas.
Claro, vc pode mudar para um do-while.
[quote=CintiaDR][code]
boolean salvo = false;
int i = 1;
while ( salvo == false){
try{
salvarArquivo(nomeDoArquivo+"("+i+")");
salvo = true;
} catch(FileException e){ // eu não sei qual exatamente é a exception que vc tem que tratar
i++;
}
}
[/code]
Só cuidado para não entrar em loop caso a excessão seja outra (sei lá, acesso a disco, etc etc). Trate as demais excessões, ou coloque um limite no número de tentativas.
Claro, vc pode mudar para um do-while. [/quote]
Fogo…
só fui jantar e o cara me tira o post dos dedos…há caras maus neste forum…hehehehe
MOÇA MÁ, hahaha moça má :lol:
(hehehe Eu achei tão boa sua idéia que ajudei o rapaz 8) )
Raarm
Novembro 27, 2007, 11:00am
#13
Vlw MOÇA MÁ…rssss Acabei chegando neste resultado, nao sei se é o melhor…mas esta atendendo!!!
File file=new File(path+name+"."+ext);
if(file.exists())
{
try
{
int i=1;
String nome=path+name+"("+i+")."+ext;
file=new File(nome);
while(file.exists())
{
nome=path+name+"("+i+")."+ext;
file=new File(nome);
i++;
}
File nfile=new File(nome);
OutputStream tmp=new FileOutputStream(nfile);
ImageCodec.createImageEncoder(ext, tmp, null).encode(bufferedImage);
tmp.close();
JOptionPane.showMessageDialog(null, "Arquivo Salvo com Sucesso!!","Arquivo",JOptionPane.INFORMATION_MESSAGE);
}catch(Throwable exception)
{
exception.printStackTrace();
System.err.println("Erro, A Imagem não pode ser salva! ..."+ exception);
}
}
else
{
OutputStream tmp=new FileOutputStream(file);
ImageCodec.createImageEncoder(ext, tmp, null).encode(bufferedImage);
tmp.close();
JOptionPane.showMessageDialog(null, "Arquivo Salvo com Sucesso!!","Arquivo",JOptionPane.INFORMATION_MESSAGE);
}
Agora estou com um problema de atualizar tabela…se der dá uma olhada…talvez vc tenha mas uma boa sugestão :oops:
http://www.guj.com.br/posts/list/75606.java#397659
Vlw Cintia!