簡易條碼產生器2 easy Barcode Genarator

本偏延續上一篇『簡易條碼產生器』的修改版

新增了指令列模式(Console Mode),方便給其它程式呼叫使用

下載位置:https://drive.google.com/drive/folders/0BybxVRTRlzdLbkVFc0JuM3JyS0E
必須有.net 2.0 以上環境(Windows 7以上作業系統不需要)
解壓縮密碼(如果需要):http://radio-idea.blogspot.tw

指令:
usage barcodegenwin.exe [-command:value] ||[-command]
-w,-width : integer value , specified picture width pixeles
-h,-height : integer value , specified picture height pixeles
-bc,-barcode : barcode type , qrcode/pdf417/code39/code128/codabar
                        if codabar the data text should start/end with A,B,C,D charcater,
                        if code39 the data text only accept Arabic number and upper letter
-txt,-text : string value , use data text to genarate barcode
-out,-output : picture file name , specified this file name to save barcode picture,
                   support format  jpeg(jpg)/Bitmap(bmp)/portable network graphics(png)
-s,-silent  keep window invisiable (no popup action)

if width,height,barcode,text,output any one loss or not be accepted , the window will popup




原始程式碼

/*Start*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;

using ZXing;
using ZXing.QrCode;
using ZXing.PDF417;
using ZXing.Common;

namespace BarcodegenWin
{
    public partial class Form1 : Form
    {
        // public variable
        string[] myArgs;
        bool[] iGrd;
        string outputFile,iExt;
     
        public Form1(string[] args)
        {
            InitializeComponent();
            myArgs = args;
            cbCharset.SelectedIndex = 0;
            cbImageType.SelectedIndex = 0;

            //use bool array to record is specified for each operator
            iGrd = new bool[] { false, false, false, false, false, false, false };
         
            //parse argument
            for (int i = 0; i < myArgs.Length; i++)
            {
                string iTxt = myArgs[i];
                string iCmd, iVal;
                int index;
                int iSep = iTxt.IndexOf(":");
                //check attach data command
                if (iSep > 0)
                {
                    iCmd = iTxt.Substring(0, iSep).ToLower();
                    iVal = iTxt.Substring(iSep + 1);
                    switch (iCmd)
                    {
                        case "-w":
                        case"-width" :
                            //set picture Width
                            tbWidth.Text = iVal;
                            iGrd[0] = true;
                            break;
                        case "-h":
                        case "-height":
                            //set picture Height
                            tbHeight.Text = iVal;
                            iGrd[1] = true;
                            break;
                        case "-bc":
                        case "-barcode":
                            //set Barcode type
                            index = cbImageType.FindString(iVal);
                            cbImageType.SelectedIndex = index;
                            if (index >= 0)
                            {
                                iGrd[2] = true;
                            }
                            break;
                        case "-cs":
                        case "-charset":
                            //no use
                            break;
                        case "-txt":
                        case "-text":
                            //set Data text
                            tbText.Text = iVal;
                            iGrd[3] = true;
                            break;
                        case "-out":
                        case "-output":
                            //set Output file
                            outputFile = iVal;
                            iExt = Path.GetExtension(iVal).ToLower();
                            iGrd[4] = true;
                            if (iExt == ".jpg" || iExt == ".bmp" || iExt == ".png")
                            {
                                iGrd[5] = true;
                            }

                            break;
                    }
                    //end switch
                }else{
                    //no attach data command
                    if (iTxt == "-s" || iTxt == "-silent")
                    {
                        this.Visible = false;
                    }
                }
                //end if
            }
            //end for
            int iGV = 0;
            for (int i = 0; i < iGrd.Length; i++)
            {
                if (iGrd[i])
                {
                    iGV++;
                }
            }
         
            if (iGV == 6)
            {
                iGrd[6] = true;
                //Let actived event handle keep go
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int iWidth, iHeight;
            string sImgType, sCharset;

            iWidth = int.Parse(tbWidth.Text);
            iHeight = int.Parse(tbHeight.Text);
            sCharset = cbCharset.Text;
            sImgType = cbImageType.Text;
            if (sImgType == "")
            {
                MessageBox.Show("no Select Barcode Image Type");
                return;
            }
            if (sCharset == "")
            {
                MessageBox.Show("no Select text charset encoding");
                return;
            }
            try
            {
                BarcodeFormat iFormat = BarcodeFormat.QR_CODE; //default
                switch (sImgType.ToLower())
                {
                    case "qrcode":
                        iFormat = BarcodeFormat.QR_CODE;
                        break;
                    case "code39":
                        iFormat = BarcodeFormat.CODE_39;
                        break;
                    case "code128":
                        iFormat = BarcodeFormat.CODE_128;
                        break;
                    case "codabar":
                        iFormat = BarcodeFormat.CODABAR;
                        break;
                    case "pdf417":
                        iFormat = BarcodeFormat.PDF_417;
                        break;
                    default:
                        MessageBox.Show(@"no match Image type operate : " + sImgType);
                        return;
                }

                var writer = new BarcodeWriter
                {
                    Format = iFormat,
                };
                switch (sImgType.ToLower())
                {
                    case "pdf417":
                        writer.Options = new PDF417EncodingOptions
                        {
                            Height = iHeight,
                            Width = iWidth,
                            CharacterSet = sCharset
                        };
                        break;
                    case "qrcode":
                        writer.Options = new QrCodeEncodingOptions
                        {
                            Height = iHeight,
                            Width = iWidth,
                            CharacterSet = sCharset
                        };
                     
                        break;
                    default:
                        writer.Options = new EncodingOptions
                        {
                            Height = iHeight,
                            Width = iWidth
                        };
                        break;
                }
                pictureBox1.Image = writer.Write(tbText.Text); //output
            }
            catch(Exception ex){
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ImageFormat iformat = ImageFormat.Bmp;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = @"JPeg Image(*.jpg)|*.jpg|Bitmap Image(*.bmp)|*.bmp|Portable Network Graphics(*.png)|*.png";
            saveFileDialog1.Title = @"Save an Image File";
            saveFileDialog1.ShowDialog();

            if (saveFileDialog1.FileName != "")
            {
                switch (saveFileDialog1.FilterIndex)
                {
                    case 1:
                        iformat = ImageFormat.Jpeg;
                        break;
                    case 2:
                        iformat = ImageFormat.Bmp;
                        break;
                    case 3:
                        iformat = ImageFormat.Png;
                        break;
                }
                pictureBox1.Image.Save(saveFileDialog1.FileName, iformat);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // check if match automatic genarate
            if (iGrd[6])
            {
                // run trigger genarator button
                button1.PerformClick();
                // save to file
                ImageFormat iformat = ImageFormat.Bmp;
                switch (iExt)
                {
                    case ".jpg":
                        iformat = ImageFormat.Jpeg;
                        break;
                    case ".bmp":
                        iformat = ImageFormat.Bmp;
                        break;
                    case ".png":
                        iformat = ImageFormat.Png;
                        break;
                }
                pictureBox1.Image.Save(outputFile, iformat);
                //ouce command mode once close
                this.Close();
            }
            else
            {
                //retore window visible (if disable by command)
                this.Visible = true;
            }
        }
    }
}
/*End*/

留言

這個網誌中的熱門文章

【研究】列印的條碼為什麼很難刷(掃描)

C# 使用 Process.Start 執行外部程式

統一發票列印小程式