Ejemplo n.º 1
0
        /// <summary>
        /// Generates a GS1-128 barcode image from the input data and returns the image as an array of bytes.
        /// </summary>
        /// <param name="data">The input data for the barcode.</param>
        /// <param name="barcodeType">The barcode type.</param>
        /// <param name="lineColor">The color of barcode lines.</param>
        /// <param name="backColor">The background color.</param>
        /// <param name="showDataLabel">Defines whether the input data is printed under the barcode lines.</param>
        /// <param name="heightToCut">The height in pixels or in percent to be cut from the top of the barcode lines to reduce the standard height.</param>
        /// <param name="scaleMultiplier">The multiplier of the barcode width for better text rendering.</param>
        /// <param name="hasQuietZones">Defines whether the barcode has quiet zones.</param>
        /// <param name="barcodeRotation">Defines barcode rotation angle.</param>
        /// <returns>Array of bytes</returns>

        internal static byte[] GetBarcodeGS1_128(string data, BarcodeType barcodeType, System.Drawing.Color lineColor, System.Drawing.Color backColor, bool showDataLabel, MeasureBarcodeUnit heightToCut, int scaleMultiplier = 2, bool hasQuietZones = true, BarcodeRotation barcodeRotation = BarcodeRotation.Clockwise_0)
        {
            // Process the data and encode it:
            byte[] encodedData = BarcodesEncoder.EncodeBarcodeDataGS1_128(data, barcodeType);

            // Create an image using encodedData and save the image to a byte array:
            byte[] imgData = BarcodesRenderer.GetBarcodeImageGS1_128(encodedData, data, heightToCut, showDataLabel, scaleMultiplier, strokeColor: lineColor, backColor: backColor, hasQuietZones, barcodeRotation);
            return(imgData);
        }
        public void Test_Renderer_GS1_128(BarcodeType type, MeasureBarcodeType heightToCutType, float heightToCutValue, int scaleMultiplier, bool hasQuiteZones, bool rotate270, string caseName)
        {
            MeasureBarcodeUnit heightToCut = new MeasureBarcodeUnit(heightToCutValue, heightToCutType);
            float labelHeight = scaleMultiplier * 2;
            int   widthShould;
            Font  labelFont = new Font(GS1_128Data.font_family_name, GS1_128Data.default_font_size * scaleMultiplier);

            labelHeight += labelFont.Size;
            BarcodeRotation rotation = BarcodeRotation.Clockwise_0;

            if (rotate270)
            {
                rotation = BarcodeRotation.Clockwise_270;
            }
            switch (type)
            {
            case BarcodeType.GS1_128A:
                if (hasQuiteZones)
                {
                    widthShould = (GS1_128Data.QuietZoneMinimumWidth * 2 + BarcodesTestData.encodedTestDataGS1_128A.Length) * scaleMultiplier;
                }
                else
                {
                    widthShould = BarcodesTestData.encodedTestDataGS1_128A.Length * scaleMultiplier;
                }
                int heightShould = (int)(widthShould * 0.15f);
                heightShould = heightShould - (int)(heightShould * BarcodesUtils.GetPercentToCut(heightShould, heightToCut, scaleMultiplier) / 100);
                heightShould = heightShould + (int)labelHeight;
                Bitmap bitmap = BarcodesRenderer.DrawBitmapGS1_128(
                    BarcodesTestData.encodedTestDataGS1_128A,
                    BarcodesTestData.inputTestStringGS1_128A,
                    heightToCut,
                    true, scaleMultiplier, null, null, hasQuiteZones, rotation);
                if (rotate270)
                {
                    bitmap.Width.Should().Be(heightShould);
                    bitmap.Height.Should().Be(widthShould);
                }
                else
                {
                    bitmap.Width.Should().Be(widthShould);
                    bitmap.Height.Should().Be(heightShould);
                }
                break;

            case BarcodeType.GS1_128B:
                if (hasQuiteZones)
                {
                    widthShould = (GS1_128Data.QuietZoneMinimumWidth * 2 + BarcodesTestData.encodedTestDataGS1_128B.Length) * scaleMultiplier;
                }
                else
                {
                    widthShould = BarcodesTestData.encodedTestDataGS1_128B.Length * scaleMultiplier;
                }
                heightShould = (int)(widthShould * 0.15f);
                heightShould = heightShould - (int)(heightShould * BarcodesUtils.GetPercentToCut(heightShould, heightToCut, scaleMultiplier) / 100);
                heightShould = heightShould + (int)labelHeight;
                bitmap       = BarcodesRenderer.DrawBitmapGS1_128(
                    BarcodesTestData.encodedTestDataGS1_128B,
                    BarcodesTestData.inputTestStringGS1_128B,
                    heightToCut,
                    true, scaleMultiplier, null, null, hasQuiteZones, rotation);
                if (rotate270)
                {
                    bitmap.Width.Should().Be(heightShould);
                    bitmap.Height.Should().Be(widthShould);
                }
                else
                {
                    bitmap.Width.Should().Be(widthShould);
                    bitmap.Height.Should().Be(heightShould);
                }
                break;

            case BarcodeType.GS1_128C:
                if (hasQuiteZones)
                {
                    widthShould = (GS1_128Data.QuietZoneMinimumWidth * 2 + BarcodesTestData.encodedTestDataGS1_128C.Length) * scaleMultiplier;
                }
                else
                {
                    widthShould = BarcodesTestData.encodedTestDataGS1_128C.Length * scaleMultiplier;
                }
                heightShould = (int)(widthShould * 0.15f);
                heightShould = heightShould - (int)(heightShould * BarcodesUtils.GetPercentToCut(heightShould, heightToCut, scaleMultiplier) / 100);
                heightShould = heightShould + (int)labelHeight;
                bitmap       = BarcodesRenderer.DrawBitmapGS1_128(
                    BarcodesTestData.encodedTestDataGS1_128C,
                    BarcodesTestData.inputTestStringGS1_128C,
                    heightToCut,
                    true, scaleMultiplier, null, null, hasQuiteZones, rotation);
                if (rotate270)
                {
                    bitmap.Width.Should().Be(heightShould);
                    bitmap.Height.Should().Be(widthShould);
                }
                else
                {
                    bitmap.Width.Should().Be(widthShould);
                    bitmap.Height.Should().Be(heightShould);
                }
                break;
            }
            Assert.True(true, caseName);
        }
        public void Test_Renderer_EAN_UPC(BarcodeType type, MeasureBarcodeType heightToCutType, float heightToCutValue, int scaleMultiplier, bool hasQuiteZones, bool rotate90, string caseName)
        {
            MeasureBarcodeUnit heightToCut = new MeasureBarcodeUnit(heightToCutValue, heightToCutType);
            BarcodeRotation    rotation    = BarcodeRotation.Clockwise_0;

            if (rotate90)
            {
                rotation = BarcodeRotation.Clockwise_90;
            }
            switch (type)
            {
            case BarcodeType.EAN_13:
                int widthShould  = EANData.default_barcode_width_EAN_13 * scaleMultiplier;
                int heightShould = BarcodesUtils.GetBarcodeHeightInt(widthShould, type);
                heightShould = heightShould - (int)(heightShould * BarcodesUtils.GetPercentToCut(heightShould, heightToCut, scaleMultiplier) / 100);
                Bitmap bitmap = BarcodesRenderer.DrawBitmapEAN_UPC(
                    BarcodesTestData.encodedTestDataEAN_13,
                    BarcodesTestData.inputTestStringEAN_13,
                    type, heightToCut, true, scaleMultiplier, null, null, hasQuiteZones, rotation);
                if (!hasQuiteZones)
                {
                    widthShould = widthShould - (EANData.left_quite_zone_count_EAN_13 + EANData.right_quite_zone_count) * scaleMultiplier;
                }
                if (rotate90)
                {
                    bitmap.Width.Should().Be(heightShould);
                    bitmap.Height.Should().Be(widthShould);
                }
                else
                {
                    bitmap.Width.Should().Be(widthShould);
                    bitmap.Height.Should().Be(heightShould);
                }
                break;

            case BarcodeType.EAN_8:
                widthShould  = EANData.default_barcode_width_EAN_8 * scaleMultiplier;
                heightShould = BarcodesUtils.GetBarcodeHeightInt(widthShould, type);
                heightShould = heightShould - (int)(heightShould * BarcodesUtils.GetPercentToCut(heightShould, heightToCut, scaleMultiplier) / 100);
                bitmap       = BarcodesRenderer.DrawBitmapEAN_UPC(
                    BarcodesTestData.encodedTestDataEAN_8,
                    BarcodesTestData.inputTestStringEAN_8,
                    type, heightToCut, true, scaleMultiplier, null, null, hasQuiteZones, rotation);
                if (!hasQuiteZones)
                {
                    widthShould = widthShould - (EANData.left_quite_zone_count_EAN_8 + EANData.right_quite_zone_count) * scaleMultiplier;
                }
                if (rotate90)
                {
                    bitmap.Width.Should().Be(heightShould);
                    bitmap.Height.Should().Be(widthShould);
                }
                else
                {
                    bitmap.Width.Should().Be(widthShould);
                    bitmap.Height.Should().Be(heightShould);
                }
                break;

            case BarcodeType.UPC_A:
                widthShould  = EANData.default_barcode_width_EAN_13 * scaleMultiplier;
                heightShould = BarcodesUtils.GetBarcodeHeightInt(widthShould, type);
                heightShould = heightShould - (int)(heightShould * BarcodesUtils.GetPercentToCut(heightShould, heightToCut, scaleMultiplier) / 100);
                bitmap       = BarcodesRenderer.DrawBitmapEAN_UPC(
                    BarcodesTestData.encodedTestDataUPC_A,
                    BarcodesTestData.inputTestStringUPC_A,
                    type, heightToCut, true, scaleMultiplier, null, null, hasQuiteZones, rotation);
                if (!hasQuiteZones)
                {
                    widthShould = widthShould - (EANData.left_quite_zone_count_EAN_13 + EANData.right_quite_zone_count) * scaleMultiplier;
                }
                if (rotate90)
                {
                    bitmap.Width.Should().Be(heightShould);
                    bitmap.Height.Should().Be(widthShould);
                }
                else
                {
                    bitmap.Width.Should().Be(widthShould);
                    bitmap.Height.Should().Be(heightShould);
                }
                break;
            }

            Assert.True(true, caseName);
        }