Pengolahan Citra Sederhana 1


Berikut adalah syntax pengolahan citra gabar Grayscale, Negative, Brightness, dan Contras:

import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class coba extends JFrame{
BufferedImage gbr;
int w;
int h;

public void baca(){
    try{
    File input = new File("1.jpg");
    gbr = ImageIO.read(input);
    w = gbr.getWidth();
    h = gbr.getHeight();
    }
    catch(Exception e){
    System.out.println("file tidak ada");
    }
}

public void tampil(){
    JFrame frame = new JFrame();
    ImageIcon image = new ImageIcon("1.jpg");
    ImageIcon image2 = new ImageIcon("fotoGray.jpg");
    ImageIcon image3 = new ImageIcon("Negative.jpg");
    ImageIcon image4 = new ImageIcon("Brightness.jpg");
    ImageIcon image5 = new ImageIcon("Contras.jpg");

    JLabel imageLabel1= new JLabel(image);
    JLabel imageLabel2= new JLabel(image2);
    JLabel imageLabel3= new JLabel(image3);
    JLabel imageLabel4= new JLabel(image4);
    JLabel imageLabel5= new JLabel(image5);
    JLabel judul = new JLabel("rossi");

    JPanel pan = new JPanel(new GridLayout(2,2));
    pan.add(imageLabel1);
    pan.add(imageLabel2);
    pan.add(imageLabel3);
    pan.add(imageLabel4);
    pan.add(imageLabel5);

    //pan.add(judul);
    frame.add(pan);
    frame.setVisible(true);
    frame.setSize(2*w+20, 2*h);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void GrayScale(){
try{
File input = new File("1.jpg");
gbr = ImageIO.read(input);
w = gbr.getWidth();
h = gbr.getHeight();
for(int i=0; i<h; i++){
                for(int j=0; j<w; j++){
                    Color c = new Color(gbr.getRGB(j, i));
                    int red = (int)(c.getRed());
                    int green = (int)(c.getGreen());
                    int blue = (int)(c.getBlue());
                    int Gray = (int)((red+green+blue)/3);
                    Color newColor = new Color(Gray, Gray, Gray);
                    gbr.setRGB(j,i,newColor.getRGB());
                }
            }
            File output = new File("FotoGray.jpg");
            ImageIO.write(gbr, "jpg", output);
        }
        catch(Exception e){
            System.out.println("File tidak ada");
        }
}

public void Negative(){
try{
File input = new File("1.jpg");
gbr = ImageIO.read(input);
w = gbr.getWidth();
h = gbr.getHeight();
for(int i=0; i<h; i++){
                for(int j=0; j<w; j++){
                    Color c = new Color(gbr.getRGB(j, i));
                    int red = (int)(c.getRed());
                    int green = (int)(c.getGreen());
                    int blue = (int)(c.getBlue());
                    int Gray = (int)((red+green+blue)/3);
                    int hasil= 255-Gray;
                    Color newColor = new Color(hasil, hasil, hasil);
                    gbr.setRGB(j,i,newColor.getRGB());
                }
            }
            File output = new File("Negative.jpg");
            ImageIO.write(gbr, "jpg", output);
        }
        catch(Exception e){
            System.out.println("File tidak ada");
        }

}

public void Brightness(){
try{
File input = new File("1.jpg");
gbr = ImageIO.read(input);
w = gbr.getWidth();
h = gbr.getHeight();
for(int i=0; i<h; i++){
                for(int j=0; j<w; j++){
                    Color c = new Color(gbr.getRGB(j, i));
                    int red = (int)(c.getRed());
                    int green = (int)(c.getGreen());
                    int blue = (int)(c.getBlue());
                    int Gray = (int)((red+green+blue)/3);
                    int hasil1= i + Gray;
                    if (hasil1 >255)
                    hasil1= 255;
                    else if (hasil1 < 0)
                    hasil1 =0;
                    Color newColor = new Color(hasil1, hasil1, hasil1);
                    gbr.setRGB(j,i,newColor.getRGB());
                }
            }
            File output = new File("Brightness.jpg");
            ImageIO.write(gbr, "jpg", output);
        }
        catch(Exception e){
            System.out.println("File tidak ada");
        }

}

public void Contras(){
try{
File input = new File("1.jpg");
gbr = ImageIO.read(input);
w = gbr.getWidth();
h = gbr.getHeight();
for(int i=0; i<h; i++){
                for(int j=0; j<w; j++){
                    Color c = new Color(gbr.getRGB(j, i));
                    int red = (int)(c.getRed());
                    int green = (int)(c.getGreen());
                    int blue = (int)(c.getBlue());
                    int Gray = (int)((red+green+blue)/3);
                    int hasil2= i * blue;
                    if (hasil2 >= 255)
                    hasil2= 255;
                    Color newColor = new Color(hasil2, hasil2, hasil2);
                    gbr.setRGB(j,i,newColor.getRGB());
                }
            }
            File output = new File("Contras.jpg");
            ImageIO.write(gbr, "jpg", output);
        }
        catch(Exception e){
            System.out.println("File tidak ada");
        }

}

 public static void main(String args[]) throws Exception {
        coba obj = new coba();
       
        obj.baca();
        obj.GrayScale();
        obj.Negative();
        obj.Brightness();
        obj.Contras();
        obj.tampil();
    }
}

Komentar

Postingan Populer