Skip to content

Instantly share code, notes, and snippets.

@mcihad
Created October 22, 2019 18:06
Show Gist options
  • Save mcihad/3e729ad93713bbbdb11c26d0a2768ae0 to your computer and use it in GitHub Desktop.
Save mcihad/3e729ad93713bbbdb11c26d0a2768ae0 to your computer and use it in GitHub Desktop.
İki resim arasındaki farkı bulma.
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
* @author Cihad GÜNDOĞDU
*/
class ImageSurface extends JPanel {
private BufferedImage image;
public ImageSurface() {
setBorder(BorderFactory.createLineBorder(Color.GRAY,1));
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D graph = (Graphics2D) g;
if (image != null) {
graph.drawImage(getImage(), 0, 0, null);
}
}
}
class ImagesPanel extends JPanel {
private ImageSurface imageSurface1;
private ImageSurface imageSurface2;
private ImageSurface imageSurfaceDiff;
public ImagesPanel() {
setBorder(new EmptyBorder(10,10,10,10));
//3 kolona böl Paneli
GridLayout layout = new GridLayout(1, 3, 5, 5);
imageSurface1 = new ImageSurface();
imageSurface2 = new ImageSurface();
imageSurfaceDiff = new ImageSurface();
setLayout(layout);
add(imageSurface1);
add(imageSurface2);
add(imageSurfaceDiff);
}
public void setImages(BufferedImage image1, BufferedImage image2) {
imageSurface1.setImage(image1);
imageSurface2.setImage(image2);
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D graph = (Graphics2D) g;
if (imageSurface1.getImage() != null && imageSurface2.getImage() != null) {
imageSurface1.repaint();
imageSurface2.repaint();
imageSurfaceDiff.repaint();
}
}
public void saveDiffImage() throws IOException {
if (imageSurfaceDiff.getImage()==null) {
JOptionPane.showMessageDialog(null, "Kaydedilecek fark resmi yok!!", "Uyarı", JOptionPane.ERROR_MESSAGE);
return;
}
JFileChooser saveDialog = new JFileChooser();
saveDialog.setAcceptAllFileFilterUsed(false);
saveDialog.setFileFilter(new FileNameExtensionFilter("Resim Dosyası","jpg","jpeg"));
if (saveDialog.showSaveDialog(null)==JFileChooser.APPROVE_OPTION) {
File file = saveDialog.getSelectedFile();
ImageIO.write(imageSurfaceDiff.getImage(),"jpeg",file);
}
}
/**
* iki resimin farkını bulan prosedür
*/
public void diffImage() {
BufferedImage image1 = imageSurface1.getImage();
BufferedImage image2 = imageSurface2.getImage();
if (image1 == null || image2 == null) {
JOptionPane.showMessageDialog(null, "İki adet resim seçilmeli", "Uyarı", JOptionPane.ERROR_MESSAGE);
return;
}
//resimlerin boyutu aynı değilse işlemi durdur
if (image1.getWidth() != image2.getWidth() && image1.getHeight() != image2.getHeight()) {
JOptionPane.showMessageDialog(null, "Resimlerin boyutları aynı olmalıdır", "Uyarı", JOptionPane.ERROR_MESSAGE);
return;
}
BufferedImage imageDiff = new BufferedImage(image1.getWidth(), image1.getHeight(), BufferedImage.TYPE_INT_RGB);
int width = image1.getWidth();
int height = image1.getHeight();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
//x,y koordinatlarındaki pikselin rengini int^32 olarak al
int rgb1 = image1.getRGB(x, y);
//Normalde ilk sekiz biti java alpha kanalına ayırır o yüzden rgb1>>24 demedim
//ilk 8 biti dikkate almadım hocam
//00000000 00000000 00000000 00000000
// Alpha Red Green Blue
//bitleri kaydırıp sırasıyla Decimal 256(0xff hex=256 decimal) değeriyle and işlemine sokuyoruz ve sadece
//ihtiyacımız olan 8 biti alıyoruz
int r1 = (rgb1 >> 16) & 0xff;
int g1 = (rgb1 >> 8) & 0xff;
int b1 = rgb1 & 0xff;
//ikinci resmin değerlerini oku
int rgb2 = image2.getRGB(x, y);
int r2 = (rgb2 >> 16) & 0xff;
int g2 = (rgb2 >> 8) & 0xff;
int b2 = rgb2 & 0xff;
//Aradaki farkın mutlak değerini al
int rDiff = 255-Math.abs(r1 - r2);
int gDiff = 255-Math.abs(g1 - g2);
int bDiff = 255-Math.abs(b1 - b2);
//Sırasıyla bitleri yerlerine göre sola kaydırıp or işlemine tabi tutarsak tam değeri elde ederiz
int diffRgb = rDiff << 16 | gDiff << 8 | bDiff;
//fark resminin pixelinde rengi ata
imageDiff.setRGB(x,y,diffRgb);
}
}
imageSurfaceDiff.setImage(imageDiff);
repaint();
}
public void clearAllImages() {
imageSurface1.setImage(null);
imageSurface2.setImage(null);
imageSurfaceDiff.setImage(null);
repaint();
}
}
public class ImageSubstractApp extends JFrame {
private File imageFile1;
private File imageFile2;
private ImagesPanel imagesPanel;
public ImageSubstractApp() {
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Resim Farkı Bulma");
setLayout(new BorderLayout());
createToolPanel();
createSurfacePanel();
}
private void createToolPanel() {
JPanel toolPanel = new JPanel();
JButton btnSelectImage = new JButton("Resimleri Seçiniz");
btnSelectImage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
imageFile1 = selectFile();
imageFile2 = selectFile();
if (imageFile1 != null && imageFile2 != null) {
try {
BufferedImage image1 = ImageIO.read(imageFile1);
BufferedImage image2 = ImageIO.read(imageFile2);
imagesPanel.setImages(image1, image2);
imagesPanel.repaint();
} catch (IOException e) {
e.printStackTrace();
} finally {
imageFile1=null;
imageFile2=null;
}
}
}
});
JButton btnSubstractImage = new JButton("Resimlerin Farkı");
btnSubstractImage.addActionListener((event) ->diffImage());
JButton btnSaveDiffImage = new JButton("Resimlerin Farkını Kaydet");
btnSaveDiffImage.addActionListener((event) -> {
try {
imagesPanel.saveDiffImage();
} catch (IOException e) {
e.printStackTrace();
}
});
JButton btnClearAllImages = new JButton("Resimleri Temizle");
btnClearAllImages.addActionListener((event) -> imagesPanel.clearAllImages());
toolPanel.add(btnSelectImage);
toolPanel.add(btnSubstractImage);
toolPanel.add(btnSaveDiffImage);
toolPanel.add(btnClearAllImages);
add(toolPanel, BorderLayout.PAGE_START);
}
private void createSurfacePanel() {
imagesPanel = new ImagesPanel();
add(imagesPanel, BorderLayout.CENTER);
}
private void diffImage() {
imagesPanel.diffImage();
}
private File selectFile() {
JFileChooser openDialog = new JFileChooser();
openDialog.setAcceptAllFileFilterUsed(false);
openDialog.setFileFilter(new FileNameExtensionFilter("Resim Seçiniz", "jpg", "jpeg"));
if (openDialog.showDialog(null, "Resim Seç") == JFileChooser.APPROVE_OPTION) {
return openDialog.getSelectedFile();
}
return null;
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new ImageSubstractApp().setVisible(true));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment