示例#1
0
        public static bool IsValidEan(string Value, BarcodeEanType EanType, out string ErrMsgIs)
        {
            ErrMsgIs = "";

            int FixedLength = 0;

            if (EanType == BarcodeEanType.Ean8)
            {
                FixedLength = 8;
            }
            else if (EanType == BarcodeEanType.Ean13)
            {
                FixedLength = 13;
            }
            else
            {
                ErrMsgIs = string.Format("잘못된 EanType:{0}입니다.", EanType);
                return(false);
            }

            if (Value.Length != FixedLength)
            {
                ErrMsgIs = string.Format("길이는 {0}자리만 허용됩니다.", FixedLength);
                return(false);
            }

            string ValueWithoutCheck = Value.Substring(0, FixedLength - 1);

            string CheckDigitRight = CValid.GetBarcodeCheckDigit(ValueWithoutCheck, out ErrMsgIs);

            if (string.IsNullOrEmpty(CheckDigitRight))
            {
                return(false);
            }

            string CheckDigit = Value.Substring(FixedLength - 1, 1);

            if (CheckDigitRight != CheckDigit)
            {
                ErrMsgIs = string.Format("잘못된 검증번호:{0}", CheckDigit);
                return(false);
            }


            return(true);
        }
示例#2
0
        //http://www.codeproject.com/Articles/10162/Creating-EAN-13-Barcodes-with-C
        /// <summary>
        /// EAN-8, EAN-13 형식으로 변환함.
        /// ean13.ttf 글꼴 필요
        /// </summary>
        /// <param name="Value"></param>
        /// <param name="EanType"></param>
        /// <returns></returns>
        public static string ToEan(string Value, BarcodeEanType EanType)
        {
            string ErrMsgIs;

            if (!IsValidEan(Value, EanType, out ErrMsgIs))
            {
                throw new Exception(ErrMsgIs);
            }

            string NewValue = "";

            if (EanType == BarcodeEanType.Ean8)
            {
                NewValue = ":";

                for (int i = 1; i <= 4; i++)
                {
                    NewValue = NewValue + Convert.ToChar(65 + Convert.ToInt32(Value.Substring(i - 1, 1)));
                }
                NewValue = NewValue + "*";
                for (int i = 5; i <= 8; i++)
                {
                    NewValue = NewValue + Convert.ToChar(97 + Convert.ToInt32(Value.Substring(i - 1, 1)));
                }
                NewValue = NewValue + "+";
            }
            else if (EanType == BarcodeEanType.Ean13)
            {
                NewValue = Value.Substring(0, 1) + Convert.ToChar(65 + Convert.ToInt32((Value.Substring(1, 1))));
                int  First = Convert.ToInt32(Value.Substring(0, 1));
                bool IsTableA;

                for (int i = 3; i <= 7; i++)
                {
                    IsTableA = false;
                    switch (i)
                    {
                    case 3:
                        switch (First)
                        {
                        case 0:
                        case 1:
                        case 2:
                        case 3:
                            IsTableA = true;
                            break;
                        }
                        break;

                    case 4:
                        switch (First)
                        {
                        case 0:
                        case 4:
                        case 7:
                        case 8:
                            IsTableA = true;
                            break;
                        }
                        break;

                    case 5:
                        switch (First)
                        {
                        case 0:
                        case 1:
                        case 4:
                        case 5:
                        case 9:
                            IsTableA = true;
                            break;
                        }
                        break;

                    case 6:
                        switch (First)
                        {
                        case 0:
                        case 2:
                        case 5:
                        case 6:
                        case 7:
                            IsTableA = true;
                            break;
                        }
                        break;

                    case 7:
                        switch (First)
                        {
                        case 0:
                        case 3:
                        case 6:
                        case 8:
                        case 9:
                            IsTableA = true;
                            break;
                        }
                        break;
                    }
                    if (IsTableA)
                    {
                        NewValue = NewValue + Convert.ToChar(65 + Convert.ToInt32(Value.Substring(i - 1, 1)));
                    }
                    else
                    {
                        NewValue = NewValue + Convert.ToChar(75 + Convert.ToInt32(Value.Substring(i - 1, 1)));
                    }
                }
                NewValue = NewValue + "*";
                for (int i = 8; i <= 13; i++)
                {
                    NewValue = NewValue + Convert.ToChar(97 + Convert.ToInt32(Value.Substring(i - 1, 1)));
                }
                NewValue = NewValue + "+";
            }


            return(NewValue);
        }