Pessoal, estou tentando inserir a funcionalidade de crop em um sisteminha que mantenho, para fazer no browser estou usando um js legal que permite que a pessoa com o cursor delimite a imagem onde vai ser cortada. Só que na hora de gerar, provavelmente não estou passando os parametros de forma correta para o metodo.
O js me retorna, x1,y1,x2,y2,largura,altura . Com isso em mãos como faria, busquei uns exemplo na net mas não rolou para mim, procurei aqui no guj, falaram do JAI, mas não consegui fazer funcionar, alguém tem algum exemplo.
Codigo
int quality = 80;
//tamanho 500 x 333
File file = new File("c:/ambiente/eclipse3.2/projetos/Cropper/web/castle.jpg");
Image image = new ImageIcon(file.toURL()).getImage();
BufferedImage thumbImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 78,49,245,155,null);
//thumbImage = thumbImage.getSubimage(80, 48, 245, 155);
BufferedOutputStream out;
try {
out = new BufferedOutputStream(new FileOutputStream("c:/ambiente/eclipse3.2/projetos/Cropper/web/castle2.jpg"));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder
.getDefaultJPEGEncodeParam(thumbImage);
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float) quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
out.close();
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException " + e.getMessage());
} catch (ImageFormatException e) {
System.out.println("ImageFormatException " + e.getMessage());
} catch (IOException e) {
System.out.println("IOException " + e.getMessage());
}
Obrigado,
Alberto
Olá
Acho que não precisa de JAI não pra isso, pode ser no Java2D mesmo.
graphics2D.drawImage(image, 78,49,245,155,null);
//thumbImage = thumbImage.getSubimage(80, 48, 245, 155);
nesse trecho, acho que vc deveria ter desenhado image na coordenada 0, 0, e aí depois fazer o getSubimage… eu digo acho porque aqui não vou conseguir fazer o teste, mas tenta aí, acho que tem chance.
No primeiro comando, você está começando a desenhar a imagem em ‘image’ a partir do ponto 78, 49 do ‘thumbImage’, ou seja, no 78,49 do ‘thumbImage’ está o 0,0 da outra.
Espero que eu tenha conseguido explicar direito… qualquer dúvida da uma olhada no javadoc que ajuda bem.
Entendi o que vc falou, alterei mas ele continuo cropando errado e continuo deixando o espaço com fundo preto no espaço que foi retirado. Com os dados que possuo, x1,y1 e por aí vai, não existe nenhum metodo que os receba e corte a imagem nas dimensões especificadas?
Alberto
Opa dudaskank, ajeitei o codigo e funcionou. Eu desenho a imagem no tamanho original, depois pego a parte requerida.
int quality = 80;
//tamanho 500 x 333
File file = new File("c:/ambiente/eclipse3.2/projetos/Croper/web/castle.jpg");
Image image = new ImageIcon(file.toURL()).getImage();
BufferedImage thumbImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0,0,image.getWidth(null),image.getHeight(null),null);
thumbImage = thumbImage.getSubimage(78, 48, 247, 84);
BufferedOutputStream out;
try {
out = new BufferedOutputStream(new FileOutputStream("c:/ambiente/eclipse3.2/projetos/EasyCM/web/castle2.jpg"));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder
.getDefaultJPEGEncodeParam(thumbImage);
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float) quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
out.close();
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException " + e.getMessage());
} catch (ImageFormatException e) {
System.out.println("ImageFormatException " + e.getMessage());
} catch (IOException e) {
System.out.println("IOException " + e.getMessage());
}