Boas,
Estou a tentar fazer um programa que pegue numa imagem e tenha a opção de desfocar ou coloca-la mais nitida...
Depois de algum tempo e pesquisa, consegui desfocar a imagem, o problema é mesmo como fazer para a tornar mais nitida, ando a volta com isto á seculos e não consigo fazer essa parte do código :? :?
Aqui fica o que eu fiz até agora:
package a.m.imagem;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
*
* @author fnac
*/
public class AMImagem {
private static RenderedImage ni;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
String nome = "";
BufferedImage image = null;
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
nome = fc.getSelectedFile().getPath();
}
else
{
System.exit(1);
}
File f1 = new File(nome);
image = ImageIO.read(f1);
//lançar imaguem
JFrame frame = new JFrame();
JLabel label = new JLabel(new ImageIcon(image));
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
int H;
int W;
H = image.getHeight(null);
W = image.getWidth(null);
int nb = image.getSampleModel().getNumBands();
int[] pixel = new int[nb];
Raster inputRaster = image.getData();
//defenir um vextor para correr tudo
int[] pixels = new int[nb * W * H];
//vai buscar todos pixeis
inputRaster.getPixels(0, 0, W, H, pixels);
//Criar nova imaguem com novas dimensoes!
BufferedImage ni = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
WritableRaster outputRaster = ni.getRaster();
for (int i = 1; i < W-1; i++) {
for (int j = 1; j < H-1; j++) {
int piexel1 = image.getRGB(i, j);
int blue1 = piexel1 & 0xFF;
int green1 = (piexel1 >> 8) & 0xFF;
int red1 = (piexel1 >> 16) & 0xFF;
int pixel2 = image.getRGB(i - 1, j - 1);
int blue2 = pixel2 & 0xFF;
int green2 = (pixel2 >> 8) & 0xFF;
int red2 = (pixel2 >> 16) & 0xFF;
int pixel3 = image.getRGB(i - 1, j);
int blue3 = pixel3 & 0xFF;
int green3 = (pixel3 >> 8) & 0xFF;
int red3 = (pixel3 >> 16) & 0xFF;
//System.out.println(i + " " + j + " " + H + " " + W);
int pixel4 = image.getRGB(i - 1, j + 1);
int blue4 = pixel4 & 0xFF;
int green4 = (pixel4 >> 8) & 0xFF;
int red4 = (pixel4 >> 16) & 0xFF;
int pixel5 = image.getRGB(i, j - 1);
int blue5 = pixel5 & 0xFF;
int green5 = (pixel5 >> 8) & 0xFF;
int red5 = (pixel5 >> 16) & 0xFF;
int pixel6 = image.getRGB(i, j + 1);
int blue6 = pixel6 & 0xFF;
int green6 = (pixel6 >> 8) & 0xFF;
int red6 = (pixel6 >> 16) & 0xFF;
int pixel7 = image.getRGB(i + 1, j - 1);
int blue7 = pixel7 & 0xFF;
int green7 = (pixel7 >> 8) & 0xFF;
int red7 = (pixel7 >> 16) & 0xFF;
int pixel8 = image.getRGB(i + 1, j);
int blue8 = pixel8 & 0xFF;
int green8 = (pixel8 >> 8) & 0xFF;
int red8 = (pixel8 >> 16) & 0xFF;
int pixel9 = image.getRGB(i + 1, j - 1);
int blue9 = pixel9 & 0xFF;
int green9 = (pixel9 >> 8) & 0xFF;
int red9 = (pixel9 >> 16) & 0xFF;
int bluefinal = (blue1 + blue2 + blue3 + blue4 + blue5 + blue6 + blue7 + blue8 + blue9) / 9;
int redfinal = (red1 + red2 + red3 + red4 + red5 + red6 + red7 + red8 + red9) / 9;
int greenfinal = (green1 + green2 + green3 + green4 + green5 + green6 + green7 + green8 + green9) / 9;
int[] color = {redfinal, greenfinal, bluefinal};
outputRaster.setPixel(i, j, color);
}
}
ni.setData(outputRaster);
ImageIO.write(ni, "JPEG", new File("image1.jpg"));
JFrame frame0 = new JFrame();
JLabel label0 = new JLabel(new ImageIcon(ni));
frame0.getContentPane().add(label0, BorderLayout.CENTER);
frame0.setLocation(W+20, 0);
frame0.pack();
frame0.setVisible(true);
}
}
Será possível alguém me ajudar nos calculos dos pixels para conseguir por a imagem mais nitida?
desde já um muito obrigado
