Kamis, 12 Januari 2017

Aplikasi Barcode Scanner dan Database

Dengan Menu-menu sebagai berikut :

Menu Login
Menu Utama
Menu Aplikasi Barcode
Menu Data Barang

Langkah Awal  buat database dan tabel sbb:

--
-- Database: `db_barcode`
--



CREATE TABLE `tb_admin` (
  `kode_admin` varchar(15) NOT NULL,
  `username` varchar(15) NOT NULL,
  `password` varchar(15) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `tb_barang`
--

CREATE TABLE `tb_barang` (
  `kode_barang` varchar(15) NOT NULL,
  `nama_barang` varchar(30) NOT NULL,
  `kategori` varchar(30) NOT NULL,
  `uraian` text NOT NULL,
  `gambar` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



Buat tampilan Login

Dengan Code sbb:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace WebcamDemo
{
    public partial class Login : Form
    {
        string kon = "server=localhost;userid=root;database=db_barcode";
      
        public Login()
        {
            InitializeComponent();
        }

        private void btnlogin_Click(object sender, EventArgs e)
        {
            try
            {
                string sql = "Select * from `tb_admin` where username = '" + txtUsername.Text + "' AND password = '" + txtpassword.Text + "'";
                MySqlConnection buatKon = new MySqlConnection(kon);
                buatKon.Open();
                DataTable dt = new DataTable();
                MySqlDataAdapter sda = new MySqlDataAdapter(sql, buatKon);
                sda.Fill(dt);

                DataSet ds = new DataSet();
                sda.Fill(ds);

                int row = ds.Tables[0].Rows.Count;
                if (row > 0)
                {
                    Menuutama frm = new Menuutama();
                    frm.Show();
                    this.SetVisibleCore(false);
                }
                else
                {
                    MessageBox.Show("Login gagal", "Fail");
                }


            }

            catch (Exception ee) { }
        }

        private void btnexit_Click(object sender, EventArgs e)
        {
            Dispose();
        }
    }
}

+++++++++++++++++++++++++++++++++++

Menu Utama:


Dengan Code sbb:(menggunakan ToolsStripMenu)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WebcamDemo
{
    public partial class Menuutama : Form
    {
        public Menuutama()
        {
            InitializeComponent();
        }

        private void adminToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormAdmin frm = new FormAdmin();
             this.Hide();
            frm.Show();
              
        }

        private void inputBarangToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormBarang  frm= new FormBarang();
            this.Hide();
            frm.Show();
        }

        private void barcodeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormBarcode frm = new FormBarcode();
            this.Hide();
            frm.Show();
        }

        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Login frm = new Login();
            this.Close();
            frm.Show();
        }
    }
}
+++++++++++++++++++++
Menu Admin :
dengan fungsi CRUD nya sbb:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MySql.Data.MySqlClient;
namespace WebcamDemo
{
    public partial class FormAdmin : Form
    {
        string kon = "server=localhost;userid=root;database=db_barcode";
    
        public FormAdmin()
        {
            InitializeComponent();
            bersih();
        }

        private void FormAdmin_Load(object sender, EventArgs e)
        {
      
        }
        void bersih()
        {
            MySqlConnection buatKon = new MySqlConnection(kon);
            buatKon.Open();
            DataTable dt = new DataTable();
            MySqlDataAdapter sda = new MySqlDataAdapter("select * from `tb_admin`", buatKon);
            sda.Fill(dt);
            dgrid.DataSource = dt;
            buatKon.Close();
            txtkode.Text = "";
            txtuser.Text = "";
            txtpass.Text = "";
            txtkode.Enabled = true;
            btnsimpan.Enabled = true;
        }
        private void btnsimpan_Click(object sender, EventArgs e)
        {
            try
            {
                String kode, user, pass;
                kode = txtkode.Text;
                user = txtuser.Text;
                pass = txtpass.Text;
                string sql = "insert into `tb_admin` (`kode_admin`, `username`,`password` ) value('" + kode + "','" + user + "','" + pass + "')";
                MySqlConnection buatKon = new MySqlConnection(kon);
                buatKon.Open();

                MySqlCommand exe = new MySqlCommand(sql, buatKon);
                MySqlDataReader simpandata = exe.ExecuteReader();

                MessageBox.Show("Save Data");
                buatKon.Close();
                bersih();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnubah_Click(object sender, EventArgs e)
        {
            try
            {
                String kode, user, pass;
                kode = txtkode.Text;
                user = txtuser.Text;
                pass = txtpass.Text;

                string sql = "UPDATE  `tb_admin` set `username`='" + user + "', `password`='" + pass + "' where `kode_admin`='" + kode + "'";

                MySqlConnection buatKon = new MySqlConnection(kon);
                buatKon.Open();

                MySqlCommand exe = new MySqlCommand(sql, buatKon);
                MySqlDataReader simpandata = exe.ExecuteReader();

                MessageBox.Show("Update Data");
                buatKon.Close();
                bersih();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnhapus_Click(object sender, EventArgs e)
        {
            try
            {
                String kode = txtkode.Text;
                string sql = "Delete from `tb_admin` where `kode_admin`='" + kode + "'";

                MySqlConnection buatKon = new MySqlConnection(kon);
                buatKon.Open();

                MySqlCommand exe = new MySqlCommand(sql, buatKon);
                MySqlDataReader simpandata = exe.ExecuteReader();

                MessageBox.Show("Delete Data");
                buatKon.Close();
                bersih();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnBersih_Click(object sender, EventArgs e)
        {
            bersih();
        }

        private void dgrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            btnhapus.Enabled = true;
            btnubah.Enabled = true;
            btnsimpan.Enabled = false;
            txtkode.Enabled = false;

            if (e.RowIndex >= 0)
            {
                DataGridViewRow row = this.dgrid.Rows[e.RowIndex];
                txtkode.Text = row.Cells["kode_admin"].Value.ToString();
                txtuser.Text = row.Cells["username"].Value.ToString();
                txtpass.Text = row.Cells["password"].Value.ToString();

            }
        }
    }
}
+++++++++++++++++++++++++++





Tampilan CRUD Barang

Uniknya adalah selain bisa CRUD aplikasi juga bisa mengcopy image atau gambar ke path active


dengan codes BB;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;

namespace WebcamDemo
{
    public partial class FormBarang : Form
    {
        String namapath = "";
        String nf="";
        String posisiImg = "";
        string kon = "server=localhost;userid=root;database=db_barcode";
        public FormBarang()
        {
            InitializeComponent();
            bersih();

        }
        void bersih()
        {
            MySqlConnection buatKon = new MySqlConnection(kon);
            buatKon.Open();
            DataTable dt = new DataTable();
            MySqlDataAdapter sda = new MySqlDataAdapter("select * from `tb_barang`", buatKon);
            sda.Fill(dt);
            dgrid.DataSource = dt;
            buatKon.Close();
            txtKodeBarang.Text = "";
            txtNamaBarang.Text = "";
            txtKategori.Text = "";
            txtUraian.Text = "";
            txtGambar.Text = "";
            txtKodeBarang.Enabled = true;
            btnSimpan.Enabled = true;
            mypic2.Image = null;
        }

        private void btnSimpan_Click(object sender, EventArgs e)
        {
            try
            {
                String kode_barang, nama_barang, kategori, uraian, gambar;
                kode_barang = txtKodeBarang.Text;
                nama_barang = txtNamaBarang.Text;
                kategori = txtKategori.Text;
                uraian = txtUraian.Text;
                gambar = txtGambar.Text;
                string sql = "insert into `tb_barang` (`kode_barang`, `nama_barang`,`kategori`,`uraian`,`gambar` ) value('" + kode_barang + "','" + nama_barang + "','" + kategori + "','" + uraian + "','" + gambar + "')";
                MySqlConnection buatKon = new MySqlConnection(kon);
                buatKon.Open();

                MySqlCommand exe = new MySqlCommand(sql, buatKon);
                MySqlDataReader simpandata = exe.ExecuteReader();

                MessageBox.Show("Simpan data berhasil");
                buatKon.Close();
                copy();
                bersih();
           
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void btnUbah_Click(object sender, EventArgs e)
        {
            try
            {
                String kode_barang, nama_barang, kategori, uraian, gambar;
                kode_barang = txtKodeBarang.Text;
                nama_barang = txtNamaBarang.Text;
                kategori = txtKategori.Text;
                uraian = txtUraian.Text;
                gambar = txtGambar.Text;

                string sql = "UPDATE  `tb_barang` set `nama_barang`='" + nama_barang + "', `kategori`='" + kategori + "', `uraian`='" + uraian + "', `gambar`='" + gambar + "' where `kode_barang`='" + kode_barang + "'";

                MySqlConnection buatKon = new MySqlConnection(kon);
                buatKon.Open();

                MySqlCommand exe = new MySqlCommand(sql, buatKon);
                MySqlDataReader simpandata = exe.ExecuteReader();

                MessageBox.Show("Ubah data berhasil");
                buatKon.Close();
                copy();
                bersih();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void btnHapus_Click(object sender, EventArgs e)
        {
            try
            {
                String kode_barang = txtKodeBarang.Text;
                string sql = "Delete from `tb_barang` where `kode_barang`='" + kode_barang + "'";

                MySqlConnection buatKon = new MySqlConnection(kon);
                buatKon.Open();

                MySqlCommand exe = new MySqlCommand(sql, buatKon);
                MySqlDataReader simpandata = exe.ExecuteReader();

                MessageBox.Show("Hapus data berhasil");
                buatKon.Close();
                bersih();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            btnHapus.Enabled = true;
            btnUbah.Enabled = true;
            btnSimpan.Enabled = false;
            txtKodeBarang.Enabled = false;

            if (e.RowIndex >= 0)
            {
                DataGridViewRow row = this.dgrid.Rows[e.RowIndex];
                txtKodeBarang.Text = row.Cells["kode_barang"].Value.ToString();
                txtNamaBarang.Text = row.Cells["nama_barang"].Value.ToString();
                txtKategori.Text = row.Cells["kategori"].Value.ToString();
                txtUraian.Text = row.Cells["uraian"].Value.ToString();
                String gbr = row.Cells["gambar"].Value.ToString();
                txtGambar.Text =gbr;

                string path = Directory.GetCurrentDirectory();
                path = path + "\\imgbar\\" + gbr;

                try
                {
                    FileStream fs = new System.IO.FileStream(path, FileMode.Open, FileAccess.Read);
                    mypic2.Image = Image.FromStream(fs);
                    fs.Close();
                }
                catch (Exception) { }

            }

        }

        private void btnBersih_Click(object sender, EventArgs e)
        {
            bersih();

        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
     
   
        OpenFileDialog open = new OpenFileDialog();
        open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.png; *.ico)|*.jpg; *.jpeg; *.gif; *.bmp; *.png; *.ico; *.bmp; *.png; *.ico";
        if (open.ShowDialog() == DialogResult.OK)
        {

            posisiImg = open.FileName;
            namapath = Path.GetDirectoryName(posisiImg);
            nf = System.IO.Path.GetFileName(posisiImg);
            txtGambar.Text = nf;
                mypic2.Image = Image.FromFile(posisiImg);
                mypic2.BackgroundImageLayout = ImageLayout.Zoom;

}
        }

        private void btnTutup_Click(object sender, EventArgs e)
        {
            Menuutama frm = new Menuutama();
            frm.Show();
            this.SetVisibleCore(false);

        }


        void copy() {
            //=====================

            string path = Directory.GetCurrentDirectory();
            path = path + "\\imgbar\\";
            string fileName = nf;
            string sourcePath = @"" + namapath;
            string targetPath = @"" + path;

            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, fileName);
            Console.WriteLine("sourcePath=" + sourcePath);
            Console.WriteLine("targetPath=" + targetPath);
            Console.WriteLine("fileName=" + fileName);
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }

            // To copy a file to another location and
            // overwrite the destination file if it already exists.
            System.IO.File.Copy(sourceFile, destFile, true);

            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);

                // Copy the files and overwrite destination files if they already exist.
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    fileName = System.IO.Path.GetFileName(s);
                    destFile = System.IO.Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(s, destFile, true);
                }
            }
            else
            {
                Console.WriteLine("Source path does not exist!");
            }

            // Keep console window open in debug mode.
            Console.WriteLine("sukses copy....");
            //=======================
        }
    }
}


Dan yang terahir barcode....aplikasi bisa memilih objek atau via camera untuk proses.....


  private void btnAnalisa_Click(object sender, EventArgs e)
        {
 BarcodeReader barcodeReader = new BarcodeReader();
            Bitmap bmp = null;
            if (this.appWebcamBarcode.CurrentImageIndexInBuffer < 0)
            {
                MessageBox.Show("Silakan capture atau pilih image terlebih dahulu.");
                return;
            }
            bmp = (Bitmap)this.appWebcamBarcode.GetImage(this.appWebcamBarcode.CurrentImageIndexInBuffer);
            BarcodeResult[] aryResult = null;
            aryResult = barcodeReader.DecodeBitmap(bmp);

            string path = Directory.GetCurrentDirectory();

            StringBuilder strText = new StringBuilder();
            if (aryResult == null)
            {
                this.txtBarcode.Text = "Barcode Tidak Terdefinisi";
                lblKode.Text = "";
                lblNama.Text = "";
                lblKategori.Text = "";
                lblUraian.Text = "";
                mypic2.Image = null;
            }
            else
            {
                for (int i = 0; i < aryResult.Length; i++)
                {
                    BarcodeResult objResult = aryResult[i];
                    String baca = objResult.BarcodeText;
                    baca = baca.Replace("***. 1D barcode license expired, please contact support@dynamsoft.com to get a valid trial license.", "");
                    strText.AppendFormat(baca);
                    if (baca.IndexOf("BRG") >= 0)
                    {
                    string sql = "select * from `tb_barang` where `kode_barang` like '%"+baca+"%'";
                    Console.WriteLine(sql);
                       MySqlConnection buatKon = new MySqlConnection(kon);
                       buatKon.Open();
                        MySqlCommand exe = new MySqlCommand(sql, buatKon);
                        MySqlDataReader reader = exe.ExecuteReader();
                        if (reader.Read()) 
                        {
                            lblKode.Text = reader.GetString(0);
                            lblNama.Text = reader.GetString(1);
                            lblKategori.Text = reader.GetString(2);
                            lblUraian.Text = reader.GetString(3);


                            String gbr = reader.GetString(4);
                         
                            string spath = Directory.GetCurrentDirectory();
                            spath = spath + "\\imgbar\\" + gbr;

                            try
                            {
                                FileStream fs = new System.IO.FileStream(spath, FileMode.Open, FileAccess.Read);
                                mypic2.Image = Image.FromStream(fs);
                                fs.Close();
                            }
                            catch (Exception) { }


                        }
                buatKon.Close();
                    }

                    this.txtBarcode.Text = strText.ToString();
                }
            }
        }
Ok selamat mencobaaaaa....



















Tidak ada komentar:

Posting Komentar