Tugas PBKK Aplikasi Webcam

Nama : Davian Benito

 Nrp : 5025201220

Kelas : PBKK-D


APLIKASI WEBCAM CAPTURE DENGAN .NET FRAMEWORK

  

Aplikasi berupa aplikasi winform yang dapat mendeteksi camera komputer, lalu menyalakannya dan mencapture image dari camera webcam tersebut yang kemudian disimpan ke dalam file .png / .jpg. 


 

 

Design aplikasi dapat dilihat seperti di gambar atas. Terdapat fitur tombol untuk memilih source video camera, lalu tombol start video, tombol capture image, dan tombol save capture. Fitur untuk mendapatkan akses ke webcam / visual komputer memakai library tambahan yaitu AForge yang didapat pada websitenya (http://www.aforgenet.com/)

Kode program pada aplikasi ini sebagai berikut :

public partial class Form1 : Form
    {
        private FilterInfoCollection captureDevice;
        private VideoCaptureDevice videoSource;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            captureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach(FilterInfo deviceList in captureDevice)
            {
                comboBoxWebcamList.Items.Add(deviceList.Name);
            }
            comboBoxWebcamList.SelectedIndex = 0;
            videoSource = new VideoCaptureDevice();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            if (videoSource.IsRunning)
            {
                videoSource.SignalToStop();
                videoSource.WaitForStop();
                pictureBox1.Image = null;
                pictureBox1.Invalidate();
            }

            videoSource = new VideoCaptureDevice(captureDevice[comboBoxWebcamList.SelectedIndex].MonikerString);
            videoSource.NewFrame += new NewFrameEventHandler(VideoSource_NewFrame);
            videoSource.Start();
        }

        private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
        }

        private void buttonCapture_Click(object sender, EventArgs e)
        {
            pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "Save Image As";
            saveFileDialog.Filter = "Image files (*.jpg, *.png) | *.jpg, *.png";
            ImageFormat imageFormat = ImageFormat.Png;

            if(saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string ext = System.IO.Path.GetExtension(saveFileDialog.FileName);
                switch (ext)
                {
                    case ".jpg":
                        imageFormat = ImageFormat.Jpeg;
                        break;
                    case ".png":
                        imageFormat = ImageFormat.Png;
                        break;
                }

                pictureBox2.Image.Save(saveFileDialog.FileName, imageFormat);
            }
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            if (videoSource.IsRunning)
            {
                videoSource.SignalToStop();
                videoSource.WaitForStop();
                pictureBox1.Image = null;
                pictureBox1.Invalidate();
                pictureBox2.Image = null;
                pictureBox2.Invalidate();
            }
            Application.Exit(null);
        }
    }

 

Dimana pada awal aplikasi dijalankan, kode program pada method 'Form1_Load' akan dijalankan. Method ini akan menginisialisasikan list dari camera yang tersedia pada komputer lalu menambahkannya ke comboBox. Sehingga user dapat langsung memilih pilihan camera yang tersedia untuk dipakai, setelah itu user dapat menekan tombol start untuk memulai camera. Method yang dijalankan, akan menambahkan frame yang didetek dari camera ke pictureBox1 setiap framenya sehingga pictureBox1 akan menampilkan video. Lalu tombol Capture akan menclone image dari pictureBox1 ke pictureBox2. Tombol save, akan menyimpan image pada pictureBox2 dengan format png / jpg pada filepath sesuai pilihan user. Terakhir adalah tombol exit yang akan menclose program. 

Ditambahakn sedikit kode tambahan untuk pengecekan saat pindah camera dan close program agar tidak terjadi error, pengecekan dilakukan bila webcan sedang berjalan, maka current videoSource akan distop, lalu pictureBox1 dan pictureBox2 akan dihilangkan. 


 


Comments