Membuat Aplikasi Webcame (Capture image & Save Image) pada Bahasa Pemograman Java

Berikut ini saya akan coba berbagi mengenai pembuatan aplikasi webcam menggunakan java. Disini saya menggunakan netbeans 6.9. Untuk penggunaan netbeans nya hampir sama untuk semua IDEnya baik yang IDE 6.9 maupun diatasnya. Langsung saja berikut ini bentuk tampilan interfacenya:

webcam1

Hasil Running Program:

webcam4

Komponen yang dibutuhkan:

  1. Dua buah jToggleButton, jToggleButton1 untuk menampilkan video camera webcamnya  dan jToggleButton2 untuk menampilkan preview image hasil capture.
  2. Dua buah jButton, jButton1 untuk tombol capture dan jButton2 untuk tombol save image hasil capture.
  3. tambahan JMF (java media framework)–> cari di google 🙂
  4. library jImage, klik disini.

Selanjutnya buat project baru dengan nama sesuai keinginan anda disini saya memberikan nama project SkaWebcam.

webcam2

Setelah itu buat interface seperti pada gambar 1 :

webcam1

Kemudian dari menu design kita beralih ke menu source, cara mengganti ke menu source dapat dilihat pada gambar dibawah ini:

webcam3

Setelah itu pada menu source program kita tambahkan method baru dengan nama :

private void initcamera() throws IOException, NoPlayerException, CannotRealizeException

Berikut source program untuk initcamera nya:

private void initcamera() throws IOException, NoPlayerException, CannotRealizeException{
 MediaLocator mlocator = new MediaLocator("vfw://0");
 player = Manager.createRealizedPlayer(mlocator);

this.jToggleButton1.add(player.getVisualComponent());
 player.start();
 }

Selanjutnya ditambahkan method setimagebutton:

private void setimagebutton(JToggleButton button, Image image){
 button.setIcon(new ImageIcon(image));
 }

Untuk source code lengkap program saya tuliskan dibawah ini, mohon diperhatikan penambahan import dan fungsi try catch dari masing-masih method.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * form_webcame.java
 *
 * Created on Jan 19, 2014, 5:44:54 PM
 */

package skawebcam;

import java.awt.Image;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.Buffer;
import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.swing.ImageIcon;
import javax.swing.JToggleButton;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import JImage.JIResizeImage;
import java.awt.image.BufferedImage;
import java.awt.FileDialog;
import java.io.File;
import javax.imageio.ImageIO;

/**
 *
 * @author sandroe
 */
public class form_webcam extends javax.swing.JFrame {

Player player;
 BufferedImage BI;
 String namafile;

/** Creates new form form_webcame */
 public form_webcam() {
 try {
 initComponents();
 initcamera();
 } catch (IOException ex) {
 Logger.getLogger(form_webcam.class.getName()).log(Level.SEVERE, null, ex);
 } catch (NoPlayerException ex) {
 Logger.getLogger(form_webcam.class.getName()).log(Level.SEVERE, null, ex);
 } catch (CannotRealizeException ex) {
 Logger.getLogger(form_webcam.class.getName()).log(Level.SEVERE, null, ex);
 }

 }

private void initcamera() throws IOException, NoPlayerException, CannotRealizeException{
 MediaLocator mlocator = new MediaLocator("vfw://0");
 player = Manager.createRealizedPlayer(mlocator);

this.jToggleButton1.add(player.getVisualComponent());
 player.start();
 }

private void setimagebutton(JToggleButton button, Image image){
 button.setIcon(new ImageIcon(image));
 }

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
 @SuppressWarnings("unchecked")
 // <editor-fold defaultstate="collapsed" desc="Generated Code">
 private void initComponents() {

jToggleButton1 = new javax.swing.JToggleButton();
 jToggleButton2 = new javax.swing.JToggleButton();
 jButton1 = new javax.swing.JButton();
 jButton2 = new javax.swing.JButton();
 jLabel1 = new javax.swing.JLabel();
 jLabel2 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 setBackground(new java.awt.Color(0, 153, 204));

jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/skawebcam/capture.png"))); // NOI18N
 jButton1.setText("Capture");
 jButton1.addActionListener(new java.awt.event.ActionListener() {
 public void actionPerformed(java.awt.event.ActionEvent evt) {
 jButton1ActionPerformed(evt);
 }
 });

jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/skawebcam/save.png"))); // NOI18N
 jButton2.setText("Save");
 jButton2.addActionListener(new java.awt.event.ActionListener() {
 public void actionPerformed(java.awt.event.ActionEvent evt) {
 jButton2ActionPerformed(evt);
 }
 });

jLabel1.setText("Sandru Kharisma");

jLabel2.setText("49013100");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
 getContentPane().setLayout(layout);
 layout.setHorizontalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createSequentialGroup()
 .addContainerGap()
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createSequentialGroup()
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createSequentialGroup()
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
 .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
 .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
 .addGap(71, 71, 71))
 .addGroup(layout.createSequentialGroup()
 .addComponent(jLabel1)
 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
 .addComponent(jToggleButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
 .addComponent(jToggleButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE))
 .addContainerGap())
 );
 layout.setVerticalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createSequentialGroup()
 .addContainerGap()
 .addComponent(jToggleButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
 .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
 .addGap(18, 18, 18)
 .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
 .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
 .addComponent(jLabel1)
 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
 .addComponent(jLabel2))
 .addGroup(layout.createSequentialGroup()
 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 .addComponent(jToggleButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)))
 .addContainerGap())
 );

pack();
 }// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
 // TODO add your handling code here:
 FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
 Buffer buffer = fgc.grabFrame();
 BufferToImage bimage = new BufferToImage((VideoFormat) buffer.getFormat());
 Image image = bimage.createImage(buffer);

JIResizeImage resize = new JIResizeImage();
 BI = (BufferedImage) image;
 Image image2 = resize.rescale(BI, 30);

this.setimagebutton(jToggleButton2, image2);
 }

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
 // TODO add your handling code here:
 FileDialog fd = new FileDialog(form_webcam.this, "Save File", FileDialog.SAVE);
 fd.show();
 if (fd.getFile() != null){
 namafile = fd.getDirectory() + fd.getFile() + ".jpg";
 try {
 File buat = new File(namafile);
 try {
 ImageIO.write(BI, "jpg", buat);
 } catch (Exception ex) {
 Logger.getLogger(form_webcam.class.getName()).log(Level.SEVERE, null, ex);
 }

} catch (Exception e) {
 }
 }
 }

/**
 * @param args the command line arguments
 */
 public static void main(String args[]) {
 java.awt.EventQueue.invokeLater(new Runnable() {
 public void run() {
 new form_webcam().setVisible(true);
 }
 });
 }

// Variables declaration - do not modify
 private javax.swing.JButton jButton1;
 private javax.swing.JButton jButton2;
 private javax.swing.JLabel jLabel1;
 private javax.swing.JLabel jLabel2;
 private javax.swing.JToggleButton jToggleButton1;
 private javax.swing.JToggleButton jToggleButton2;
 // End of variables declaration

}

Berikut ini video tutorial pembuatan aplikasinya :

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s