Ads 468x60px

Minggu, 19 Mei 2013

PROGRAM JAVA KONTRAS CITRA -PENGOLAHAN CITRA SHARPEN DAN BLUR

SM Society, kali ini akan memposting artikel tentang program java yang berkaitan dengan pengolahan citra digital yaitu Sharpen dan Blur, dimana program ini mengubah warna citra sebuah gambar.

Berikut Listing programnya, dengan menggunakan JCreatro sebagai aplikasi untuk menjalankan dan melihat hasilnya. Dan kemudian simpan dengan extensi Kontras_Citra.java :



import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import javax.swing.border.TitledBorder;
public class Kontras_Citra extends JFrame {
CPanel displayPanel;
JButton sharpenButton, blurringButton, edButton, resetButton;
public Kontras_Citra() {
super();
Container ct = getContentPane();
displayPanel = new CPanel();
ct.add(displayPanel);

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 2));
panel.setBorder(new TitledBorder("Click button to perform"));

sharpenButton = new JButton("Sharpen");
sharpenButton.addActionListener(new buttonListener());
blurringButton = new JButton("Blur");
blurringButton.addActionListener(new buttonListener());
resetButton = new JButton("Reset");
resetButton.addActionListener(new buttonListener());

panel.add(sharpenButton);
panel.add(blurringButton);
panel.add(resetButton);

ct.add(BorderLayout.SOUTH, panel);
addWindowListener(new  WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

setSize(displayPanel.getWidth(), displayPanel.getHeight() + 10);
setVisible(true);
}

public static void main(String arg[]) {
new Kontras_Citra();
}

class buttonListener implements ActionListener {
public void actionPerformed(java.awt.event.ActionEvent e) {
JButton bt = (JButton) e.getSource();

if(bt.equals(sharpenButton)) {
displayPanel.sharpen();
displayPanel.repaint();
}

else if(bt.equals(blurringButton)) {
displayPanel.blur();
displayPanel.repaint();
}

else if(bt.equals(resetButton)) {
displayPanel.reset();
displayPanel.repaint();
}
}
}
}

class CPanel extends JLabel {
Image displayImage;
BufferedImage biS; 
BufferedImage biD;
BufferedImage bi;
Graphics2D big;

CPanel() {
setBackground(Color.white);
loadImage();
setSize(displayImage.getWidth(this), displayImage.getWidth(this));
createBufferedImage();
bi = biS;
}

public void loadImage() {
displayImage = Toolkit.getDefaultToolkit().getImage("1.jpg");
MediaTracker mt = new MediaTracker(this);
mt.addImage(displayImage, 1);
try {
mt.waitForAll();
}
  catch(Exception e) {
  System.out.println("Exception");
}

if (displayImage.getWidth(this) == -1) {
System.out.println("No JPG File");
System.exit(0);
}
}

public void createBufferedImage() {
biS = new BufferedImage(displayImage.getWidth(this), displayImage.getHeight(this), BufferedImage.TYPE_INT_RGB);
big = biS.createGraphics();
big.drawImage(displayImage, 0, 0, this);
biD = new BufferedImage(displayImage.getWidth(this), displayImage.getHeight(this), BufferedImage.TYPE_INT_RGB);
}

public void sharpen() {
float data[] = {-1.0f, -1.0f, -1.0f, -1.0f, 9.0f, -1.0f, -1.0f, -1.0f, -1.0f};
Kernel ker = new Kernel(3, 3, data);
ConvolveOp conv = new ConvolveOp(ker, ConvolveOp.EDGE_NO_OP, null);
conv.filter(biS, biD);
bi = biD;
}

public void blur() {
float data[] = {0.0626f, 0.125f, 0.0625f, 0.125f, 0.25f, 0.125f, 0.0625f, 0.125f, 0.0625f};
Kernel ker = new Kernel(3, 3, data);
ConvolveOp conv = new ConvolveOp(ker, ConvolveOp.EDGE_NO_OP, null);
conv.filter(biS, biD);
bi = biD;
}

public void reset() {
big.setColor(Color.black);
big.clearRect(0, 0, bi.getWidth(this), bi.getHeight(this));
big.drawImage(displayImage, 0,0, this);
bi = biS;
}
public void update(Graphics g) {
g.clearRect(0, 0, getWidth(), getHeight());
paintComponent(g);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(bi, 0, 0, this);
}
 }
Hasil Outputnya :


Gambar Beriku merupanak foto, yang akan disimpan didalam folder yang sama dengan program!

Ini hasil dari program setelah dieksekusi.

Hasil jika click button blur, hasilnya agak kabur.

Hasil setelah melakukan sharpen.

0 Komentar: