Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
115 views

Contoh Program Thresholding Menggunakan Bahasa Pemrograman Java Screen Shot

This document provides code for a Java program that performs thresholding on images. The program allows the user to open an image file, select a threshold value, and convert the image to black and white based on that threshold. The code includes classes for the main form, an image chooser, image filter, and main method. It uses common Java libraries like Swing, AWT, and ImageIO to build the GUI and perform the image processing.

Uploaded by

Ezhu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views

Contoh Program Thresholding Menggunakan Bahasa Pemrograman Java Screen Shot

This document provides code for a Java program that performs thresholding on images. The program allows the user to open an image file, select a threshold value, and convert the image to black and white based on that threshold. The code includes classes for the main form, an image chooser, image filter, and main method. It uses common Java libraries like Swing, AWT, and ImageIO to build the GUI and perform the image processing.

Uploaded by

Ezhu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CONTOH PROGRAM THRESHOLDING

MENGGUNAKAN BAHASA PEMROGRAMAN JAVA

SCREEN SHOT :

Gambar (1) Tampilan Awal Program

Gambar (2) Tampilan Menu “Open” File


Gambar (3) Tampilan gambar yang dipilih

Gambar (4) Tampilan gambara yang telah di konversi


CODING :

a. Form.java

package app.java.thresholding;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class Form extends javax.swing.JFrame implements ActionListener{

ImageChooser imgChooser = new ImageChooser();


/** Creates new form Form */
public Form() {
setTitle("Thresholding Processing With Java");

initComponents();
itemOpen.setActionCommand("open");
itemOpen.addActionListener(this);
}

private void btnConvertActionPerformed (java.awt.event.ActionEvent evt)


{
try
{
File fail = imgChooser.getSelectedFile ();
BufferedImage pic = ImageIO.read (fail);
int threshold = 127;
int red;
int green;
int blue;
int rgb;
int gray = 0;
int width = pic.getWidth ();
int height = pic.getHeight ();
BufferedImage im = new BufferedImage (width,height,BufferedImage.TYPE_BYTE_GRAY);
WritableRaster raster = im.getRaster ();

for(int i=0; i<pic.getWidth (); i++)


{
for(int j=0; j<pic.getHeight (); j++)
{
rgb = pic.getRGB (i, j);
red = rgb & 0x000000ff;
green = (rgb & 0x0000ff00) >> 8;
blue = (rgb & 0x00ff0000) >> 16;
double avg = (double) (0.299 * red + 0.587 * green + 0.114 * blue);
if (avg > threshold)
{
gray = 255;
}
else if (avg < threshold)
{
gray = 0;
}
int biner = gray+(gray<<8)+(gray<<16);
raster.setSample (i, j, 0, biner);
}
}
ImageIO.write (im,"PNG",new File ("newGray.png"));
ImageIcon ikon = new ImageIcon ("newGray.png");
lblGray.setIcon (ikon);
}
catch (IOException ex)
{
Logger.getLogger (Form.class.getName ()).log (Level.SEVERE, null, ex);
}
}

private void itemExitActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
System.exit(0);
}

public void actionPerformed(ActionEvent e) {


int result;
if (e.getActionCommand().equals("open")) {
result = imgChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = imgChooser.getSelectedFile();
ImageIcon icon = new ImageIcon(file.getPath());
lblRgb.setIcon(icon);
}
}
}

}
b. imageChooser.java
package app.java.thresholding;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ImageChooser extends JFileChooser{

private JLabel labelImage;


public ImageChooser() {
super();
setFileFilter(new ImageFilter());

labelImage = new JLabel("", JLabel.CENTER);

JScrollPane pane = new JScrollPane(labelImage);

JPanel panel = new JPanel(new java.awt.BorderLayout());


panel.setPreferredSize(new java.awt.Dimension(300, 300));
panel.add(pane);

setAccessory(panel);

addPropertyChangeListener(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY, new
PropertyChangeListener() {

public void propertyChange(PropertyChangeEvent evt) {


if (getSelectedFile() != null) {
if (!getSelectedFile().isDirectory()) {
ImageIcon icon = new ImageIcon(getSelectedFile().getPath());
labelImage.setIcon(icon);
}
}
}
});
}
}
c. ImageFilter.java
package app.java.thresholding;

import java.io.File;
import javax.swing.filechooser.FileFilter;

public class ImageFilter extends FileFilter{

@Override
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
String name = f.getName().toUpperCase();
if (name.endsWith(".JPG") || name.endsWith(".PNG") || name.endsWith(".GIF") ||
name.endsWith(".BMP")) {
return true;
}
return false;
}

@Override
public String getDescription() {
return "Image File (JPG, PNG, GIF, BMP)";
}
}

d. Main.java
public class Main {
private static Form form;
public static void main(String[] args) {
WidgetUtilities.invokeLater(new Runnable() {

public void run() {


try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
form = new Form();
form.setVisible(true);
}
});
}
}

You might also like