Kali ini ane mau share program citra digital. Langsung aj yah...
Program ini meliputi RGB, GrayScale, B/W, Negasi, D.Aritmatika, D.Geometri dan Jarak Pixel
import java.io.*;
import java.math.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.*;
/*================================================
|| PENGOLAH CITRA DIGITAL ||
|| KELAS B ||
|| MASKUR (06.2009.1.04860) ||
|| SEPTIAN SATRYA NUGRAHA (06.2009.1.04875) ||
================================================*/
public class imageprocessing extends JFrame implements ActionListener
{
JButton open = new JButton("Open");
JButton exit = new JButton("Exit");
JButton rgb = new JButton("RGB");
JButton gray = new JButton("GrayScale");
JButton negasi = new JButton("Negasi");
JButton bw = new JButton ("Black & White");
JButton jp = new JButton ("Jarak Pixel");
JButton arit = new JButton("G. Aritmatika");
JButton geo = new JButton("G. Geometrik");
JButton ntsc = new JButton("G. NTSC");
JPanel ruru = new JPanel();
pic sebelumIMG, sesudahIMG;
BufferedImage B,B2,B3;
JFileChooser fc;
JMenuBar mb;
JMenu mn,mno;
JMenuItem mi;
Container cp;
public imageprocessing()
{
getContentPane().setLayout(null);
setLocation(80,100);
open.addActionListener(this);
add(open);
ruru.add(open);
rgb.addActionListener(this);
add(rgb);
ruru.add(rgb);
gray.addActionListener(this);
add(gray);
ruru.add(gray);
bw.addActionListener(this);
add(bw);
ruru.add(bw);
negasi.addActionListener(this);
add(negasi);
ruru.add(negasi);
arit.addActionListener(this);
add(arit);
ruru.add(arit);
geo.addActionListener(this);
add(geo);
ruru.add(geo);
jp.addActionListener(this);
add(jp);
ruru.add(jp);
exit.addActionListener(this);
add(exit);
ruru.add(exit);
ruru.setLayout(new GridLayout(14,1));
getContentPane().add(ruru);
setTitle("Program Image Processing");
fc = new JFileChooser();
cp = getContentPane();
cp.setLayout(new FlowLayout());
sebelumIMG = new pic();
sesudahIMG = new pic();
cp.add(sebelumIMG);
cp.add(sesudahIMG);
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource().equals(open))
{
int op = fc.showOpenDialog(this);
if (op==JFileChooser.APPROVE_OPTION)
{
try
{
B = ImageIO.read(fc.getSelectedFile());
sebelumIMG.setImage(B);
pack();
}
catch(IOException e)
{
System.out.println("Error");
}
}
else
{
JOptionPane.showMessageDialog(null,"Belum Ada Gambar!","WARNING",JOptionPane.WARNING_MESSAGE);
}
}
else if (ae.getSource().equals(gray))
{
if (B==null)
{
JOptionPane.showMessageDialog(null,"Tidak Ada Gambar Untuk Diproses","WARNING",JOptionPane.WARNING_MESSAGE);
}
else
{
B3=GRAY(B);
sesudahIMG.setImage(B3);
pack();
}
}
else if (ae.getSource().equals(negasi))
{
if (B==null)
{
JOptionPane.showMessageDialog(null,"Tidak Ada Gambar Untuk Diproses","WARNING",JOptionPane.WARNING_MESSAGE);
}
else
{
B3=NEGASI(B);
sesudahIMG.setImage(B3);
pack();
}
}
else if (ae.getSource().equals(rgb))
{
if (B==null){
JOptionPane.showMessageDialog(null,"Tidak Ada Gambar Untuk Diproses","WARNING",JOptionPane.WARNING_MESSAGE);
}else{
RGB(B);
B3=B;
sesudahIMG.setImage(B3);
pack();
}
}
else if (ae.getSource().equals(jp))
{
if (B==null){
JOptionPane.showMessageDialog(null,"Tidak Ada Gambar Untuk Diproses","WARNING",JOptionPane.WARNING_MESSAGE);
}
else
{
JP(B);
}
}
else if (ae.getSource().equals(bw))
{
if (B==null)
{
JOptionPane.showMessageDialog(null,"Anda Belum memilih gambar untuk diproses","warning",JOptionPane.WARNING_MESSAGE);
}
else
{
B3=BI(B);
sesudahIMG.setImage(B3);
pack();
}
}
else if (ae.getSource().equals(arit))
{
if (B==null)
{
JOptionPane.showMessageDialog(null,"Tidak Ada Gambar Untuk Diproses","WARNING",JOptionPane.WARNING_MESSAGE);
}
else
{
B3=ARIT(B);
sesudahIMG.setImage(B3);
pack();
}
}
else if (ae.getSource().equals(geo))
{
if (B==null)
{
JOptionPane.showMessageDialog(null,"Tidak Ada Gambar Untuk Diproses","WARNING",JOptionPane.WARNING_MESSAGE);
}
else
{
B3=GEO(B);
sesudahIMG.setImage(B3);
pack();
}
}
else
{
System.exit(0);
}
}
public BufferedImage NEGASI(BufferedImage B)
{
BufferedImage dest = new BufferedImage(B.getWidth(), B.getHeight(), BufferedImage.TYPE_INT_RGB);
int gray = 0;
System.out.println("");
System.out.println("========");
System.out.println("*NEGASI*");
System.out.println("========");
System.out.println("RED");
System.out.println("===");
for (int y = 0; y < B.getHeight(); y++)
{
for (int x = 0; x < B.getWidth(); x++)
{
int rgb = B.getRGB(x, y);
int red = rgb & 0x000000FF;
int green = (rgb & 0x0000FF00) >> 8;
int blue = (rgb & 0x00FF0000) >> 16;
int merah = 255-red;
int hijau =255-green;
int biru = 255-blue;
int biner = merah+(hijau<<8)+(biru<<16);
System.out.print(merah+" ");
dest.setRGB(x, y, biner);
}
System.out.println("");
}
System.out.println("GREEN");
System.out.println("=====");
for (int y = 0; y < B.getHeight(); y++)
{
for (int x = 0; x < B.getWidth(); x++)
{
int rgb = B.getRGB(x, y);
int red = rgb & 0x000000FF;
int green = (rgb & 0x0000FF00) >> 8;
int blue = (rgb & 0x00FF0000) >> 16;
int merah = 255-red;
int hijau =255-green;
int biru = 255-blue;
int biner = merah+(hijau<<8)+(biru<<16);
System.out.print(hijau+" ");
dest.setRGB(x, y, biner);
}
System.out.println("");
}
System.out.println("BLUE");
System.out.println("====");
for (int y = 0; y < B.getHeight(); y++)
{
for (int x = 0; x < B.getWidth(); x++)
{
int rgb = B.getRGB(x, y);
int red = rgb & 0x000000FF;
int green = (rgb & 0x0000FF00) >> 8;
int blue = (rgb & 0x00FF0000) >> 16;
int merah = 255-red;
int hijau =255-green;
int biru = 255-blue;
int biner = merah+(hijau<<8)+(biru<<16);
System.out.print(biru+" ");
dest.setRGB(x, y, biner);
}
System.out.println("");
}
System.out.println("");
return dest;
}
public BufferedImage GRAY(BufferedImage end)
{
BufferedImage dest = new BufferedImage(end.getWidth(), end.getHeight(), BufferedImage.TYPE_INT_RGB);
System.out.println("");
System.out.println("======");
System.out.println("*GRAY*");
System.out.println("======");
for (int y = 0; y < end.getHeight(); y++)
{
for (int x = 0; x < end.getWidth(); x++)
{
int rgb = end.getRGB(x, y);
int alpha = (rgb << 24) & 0xFF;
int red = rgb & 0x000000FF;
int green = (rgb & 0x0000FF00) >> 8;
int blue = (rgb & 0x00FF0000) >> 16;
int avg = (red + green + blue) / 3;
System.out.print(avg+" ");
int gray = alpha | avg << 16 | avg << 8 | avg;
dest.setRGB(x, y, gray);
}
System.out.println();
}
System.out.println("");
return dest;
}
public BufferedImage ARIT(BufferedImage end)
{
BufferedImage dest = new BufferedImage(end.getWidth(), end.getHeight(), BufferedImage.TYPE_INT_RGB);
System.out.println("");
System.out.println("============");
System.out.println("*ARITMATIKA*");
System.out.println("============");
for (int y = 0; y < end.getHeight(); y++)
{
for (int x = 0; x < end.getWidth(); x++)
{
int rgb = end.getRGB(x, y);
int alpha = (rgb << 24) & 0xFF;
int red = rgb & 0x000000FF;
int green = (rgb & 0x0000FF00) >> 8;
int blue = (rgb & 0x00FF0000) >> 16;
int avg = (red + green + blue) / 3;
System.out.print(avg+" ");
int gray = alpha | avg << 16 | avg << 8 | avg;
dest.setRGB(x, y, gray);
}
System.out.println();
}
System.out.println("");
return dest;
}
public BufferedImage GEO(BufferedImage end)
{
BufferedImage dest = new BufferedImage(end.getWidth(), end.getHeight(), BufferedImage.TYPE_INT_RGB);
System.out.println("");
System.out.println("==========");
System.out.println("*GEOMETRI*");
System.out.println("==========");
for (int y = 0; y < end.getHeight(); y++)
{
for (int x = 0; x < end.getWidth(); x++)
{
int rgb = end.getRGB(x, y);
int alpha = (rgb << 24) & 0xFF;
int red = rgb & 0x000000FF;
int green = (rgb & 0x0000FF00) >> 8;
int blue = (rgb & 0x00FF0000) >> 16;
int avg = (int)Math.pow((red*green*blue),0.33333); //pembulatan ke bawah
System.out.print(avg+" ");
int gray = alpha | avg << 16 | avg << 8 | avg;
dest.setRGB(x, y, gray);
}
System.out.println();
}
System.out.println("");
return dest;
}
public void RGB(BufferedImage end)
{
System.out.println("=====");
System.out.println("*RGB*");
System.out.println("=====");
System.out.println("RED");
System.out.println("====");
for (int y = 0; y < end.getHeight(); y++)
{
for (int x = 0; x < end.getWidth(); x++)
{
int rgb = end.getRGB(x, y);
int red = rgb & 0x000000FF;
int green = (rgb & 0x0000FF00) >> 8;
int blue = (rgb & 0x00FF0000) >> 16;
System.out.print(red+" ");
}
System.out.println();
}
System.out.println("GREEN");
System.out.println("====");
for (int y = 0; y < end.getHeight(); y++)
{
for (int x = 0; x < end.getWidth(); x++)
{
int rgb = end.getRGB(x, y);
int red = rgb & 0x000000FF;
int green = (rgb & 0x0000FF00) >> 8;
int blue = (rgb & 0x00FF0000) >> 16;
System.out.print(green+" ");
}
System.out.println();
}
System.out.println("BLUE");
System.out.println("====");
for (int y = 0; y < end.getHeight(); y++)
{
for (int x = 0; x < end.getWidth(); x++)
{
int rgb = end.getRGB(x, y);
int red = rgb & 0x000000FF;
int green = (rgb & 0x0000FF00) >> 8;
int blue = (rgb & 0x00FF0000) >> 16;
System.out.print(blue+" ");
}
System.out.println();
}
System.out.println("");
}
public BufferedImage BI(BufferedImage end)
{
System.out.println("");
System.out.println("================");
System.out.println("*BLACK AND WHITE*");
System.out.println("================");
BufferedImage dest = new BufferedImage(end.getWidth(), end.getHeight(), BufferedImage.TYPE_INT_RGB);
int gray = 0;
int satu = 0;
for (int y = 0; y < end.getHeight(); y++) //ketika coloumn, kebawah(2)
{
for (int x = 0; x < end.getWidth(); x++) //ketika baris, kesamping (1)
{
int rgb = end.getRGB(x, y);
int red = rgb & 0x000000FF;
int green = (rgb & 0x0000FF00) >> 8;
int blue = (rgb & 0x00FF0000) >> 16;
int avg = (red + green + blue) / 3; //rata-rata
if(avg < 128) //256:2
{
gray = 0;
satu = 0;
}
else if(avg >= 128)
{
gray = 255;
satu = 1;
}
System.out.print(satu+" ");
int biner = gray+(gray<<8)+(gray<<16);
dest.setRGB(x, y, biner);
}
System.out.println();
}
System.out.println("");
return dest;
}
public void JP(BufferedImage end)
{
DataInputStream dis = new DataInputStream (System.in);
try
{
System.out.println("");
System.out.println("===============================");
System.out.println("*MENGHITUNG JARAK ANTAR PIKSEL*");
System.out.println("===============================");
int xx,yy,s,t,euc,city,chess,euc1;
System.out.print("Nilai x = ");
xx = Integer.parseInt(dis.readLine());
System.out.print("Nilai s = ");
s = Integer.parseInt(dis.readLine());
System.out.print("Nilai y = ");
yy = Integer.parseInt(dis.readLine());
System.out.print("Nilai t = ");
t = Integer.parseInt(dis.readLine());
euc = (int)Math.pow(((xx-s)*(xx-s))+((yy-t)*(yy-t)),0.5);
city = Math.abs(xx-s) + Math.abs(yy-t); //mutlak(nilai tidak akan positif)
if (Math.abs(xx-s) > Math.abs(yy-t)) //mencari nilai max
{
chess = Math.abs(xx-s);
}
else
{
chess = Math.abs(yy-t);
}
System.out.println();
System.out.println("Dengan Perhitungan Euclidean = "+euc);
System.out.println("Dengan Perhitungan City - Block = "+city);
System.out.println("Dengan Perhitungan Chessboard = "+chess);
System.out.println("");
}
catch (Exception E)
{
}
}
public static void main(String[] args)
{
JFrame saya = new imageprocessing();
saya.setDefaultCloseOperation(EXIT_ON_CLOSE);
saya.setVisible(true);
saya.pack();
}
}
class pic extends JPanel
{
BufferedImage B;
public pic()
{
B=null;
setPreferredSize(new Dimension(300, 350));
}
public pic(BufferedImage B)
{
this.B = B;
int h = B.getHeight();
int w = B.getWidth();
setSize(w,h);
}
public void paintComponent(Graphics g)
{
int i;
Graphics2D g2 = (Graphics2D) g;
Paint lama = g2.getPaint();
g2.setColor(Color.white);
g2.fillRect(0,0,getWidth(),getHeight());
g2.setColor(Color.black);
int xc=500;
int yc=370;
Graphics2D g4 = (Graphics2D) g;
g4.setColor(Color.white);
if (B != null)
{
g4.drawImage(B, 0, 0, getWidth(), getHeight(), this);
}
else
{
g4.drawRect(0, 0, getWidth(), getHeight());
}
}
public void setImage(BufferedImage B)
{
this.B = B;
invalidate();
repaint();
}
}
Hasil Output:
Demikian Sekian dan terima kasih...Bila ada pertanyaan Monggo sedoyo
comment...Tapi ingat...belajar memahami lebih baik daripada hanya
mengcopy-paste saja...So...Pahamilah......
Siiip infonya. Walaupun ane ndak paham :D :D :D
BalasHapus