Beispiel #1
0
        /// <summary>
        /// Encodes data into a DataMatrix barcode.
        /// </summary>
        /// <param name="data">The data to encode.</param>
        /// <param name="options">The options used for encoding.</param>
        /// <returns>The results from encoding.</returns>
        /// <example>
        /// This example shows a basic encoding.
        /// <code>
        ///   byte[] data = Encoding.ASCII.GetBytes("test");
        ///   EncodeOptions o = new EncodeOptions();
        ///   DmtxEncoded encodeResults = Dmtx.Encode(data, o);
        ///   encodeResults.Bitmap.Save("barcode.bmp", ImageFormat.Bmp);
        /// </code>
        /// </example>
        public static DmtxEncoded Encode(byte[] data, EncodeOptions options)
        {
            IntPtr result;
            byte   status;

            try {
                status = DmtxEncode(data, (UInt16)data.Length, out result, options);
            } catch (Exception ex) {
                throw new DmtxException("Encoding error.", ex);
            }
            if (status == RETURN_NO_MEMORY)
            {
                throw new DmtxOutOfMemoryException("Not enough memory.");
            }
            else if (status == RETURN_INVALID_ARGUMENT)
            {
                throw new DmtxInvalidArgumentException("Invalid options configuration.");
            }
            else if (status == RETURN_ENCODE_ERROR)
            {
                throw new DmtxException("Error while encoding.");
            }
            else if ((status > 0) || (result == IntPtr.Zero))
            {
                throw new DmtxException("Unknown error.");
            }

            DmtxEncoded     ret;
            EncodedInternal intResult = null;

            try {
                intResult      = (EncodedInternal)Marshal.PtrToStructure(result, typeof(EncodedInternal));
                ret            = new DmtxEncoded();
                ret.SymbolInfo = intResult.SymbolInfo;
                ret.Bitmap     = new Bitmap((int)intResult.Width, (int)intResult.Height, PixelFormat.Format24bppRgb);
                Rectangle  rect = new Rectangle(0, 0, ret.Bitmap.Width, ret.Bitmap.Height);
                BitmapData bd   = ret.Bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
                try {
                    DmtxCopyEncodeResult(intResult.Data, (UInt32)Math.Abs(bd.Stride), bd.Scan0);
                } finally {
                    ret.Bitmap.UnlockBits(bd);
                }
            } catch (Exception ex) {
                throw new DmtxException("Error parsing encode result.", ex);
            } finally {
                try {
                    if (intResult != null)
                    {
                        DmtxFreeEncodeResult(intResult.Data);
                    }
                } catch (Exception ex) {
                    throw new DmtxException("Error freeing memory.", ex);
                }
            }
            return(ret);
        }
Beispiel #2
0
        public void TestEncode()
        {
            Bitmap expectedBitmap = GetBitmapFromResource("Libdmtx.TestImages.Test001.png");

            byte[]        data          = Encoding.ASCII.GetBytes("Test");
            EncodeOptions opt           = new EncodeOptions();
            DmtxEncoded   encodeResults = Dmtx.Encode(data, opt);

            Assert.IsNotNull(encodeResults);
            AssertAreEqual(expectedBitmap, encodeResults.Bitmap);
        }
Beispiel #3
0
        private void button2_Click(object sender, EventArgs e)
        {
            //// C# Example
            Libdmtx.EncodeOptions o = new Libdmtx.EncodeOptions();
            byte[] dataToEncode     = Encoding.ASCII.GetBytes("Hello World!");
            Libdmtx.DmtxEncoded en  = Dmtx.Encode(dataToEncode, o);
            pictureBox1.Image = en.Bitmap;

            //byte b  = dmtxEncodeCreate();
            //string s = dmtxVersion();
        }
Beispiel #4
0
        public void TestStrideAndPadding()
        {
            EncodeOptions encodeOptions = new EncodeOptions {
                MarginSize = 2,
                ModuleSize = 2
            };
            DmtxEncoded encoded = Dmtx.Encode(Encoding.ASCII.GetBytes("t"), encodeOptions);
            Bitmap      bm      = encoded.Bitmap;

            // make sure we have an image who's stride is not divisable by 3
            int stride;

            ExecuteBitmapToByteArray(bm, out stride);
            if (stride % 3 == 0)
            {
                bm = BitmapIncreaseCanvas(bm, bm.Width + 1, bm.Height, Color.White);
                ExecuteBitmapToByteArray(bm, out stride);
            }
            Assert.AreNotEqual(0, stride % 3, "Stride was divisable by 3 which doesn't make a very good test");

            DecodeOptions opt = new DecodeOptions();
            Bitmap        diagnoseImage;

            DmtxDecoded[] decodedImages = Dmtx.Decode(bm, opt, DiagnosticImageStyles.Default, out diagnoseImage);

            //diagnoseImage.Save("c:/temp/diagnose.bmp", ImageFormat.Bmp);

            Assert.AreEqual(24.0, diagnoseImage.Width, 1.0);
            Assert.AreEqual(24.0, diagnoseImage.Height, 1.0);

            Assert.AreEqual(1, decodedImages.Length, "Didn't find barcode");

            // make sure the left line is straight up and down (not skewed)
            for (int y = 4; y < diagnoseImage.Height - 4; y++)
            {
                Color clrLeft  = diagnoseImage.GetPixel(1, y);
                Color clrRight = diagnoseImage.GetPixel(3, y);
                Assert.AreEqual(1.0, clrLeft.GetBrightness(), 0.01, "at location [1, " + y + "]");
                Assert.AreEqual(0.698, clrRight.GetBrightness(), 0.01, "at location [3, " + y + "]");
            }
        }
        /// <summary>
        /// Encodes data into a DataMatrix barcode.
        /// </summary>
        /// <param name="data">The data to encode.</param>
        /// <param name="options">The options used for encoding.</param>
        /// <returns>The results from encoding.</returns>
        /// <example>
        /// This example shows a basic encoding.
        /// <code>
        ///   byte[] data = Encoding.ASCII.GetBytes("test");
        ///   EncodeOptions o = new EncodeOptions();
        ///   DmtxEncoded encodeResults = Dmtx.Encode(data, o);
        ///   encodeResults.Bitmap.Save("barcode.bmp", ImageFormat.Bmp);
        /// </code>
        /// </example>
        public static DmtxEncoded Encode(byte[] data, EncodeOptions options)
        {
            IntPtr result;
            byte status;
            try {
                status = DmtxEncode(data, (UInt16)data.Length, out result, options);
            } catch (Exception ex) {
                throw new DmtxException("Encoding error.", ex);
            }
            if (status == RETURN_NO_MEMORY) {
                throw new DmtxOutOfMemoryException("Not enough memory.");
            } else if (status == RETURN_INVALID_ARGUMENT) {
                throw new DmtxInvalidArgumentException("Invalid options configuration.");
            } else if (status == RETURN_ENCODE_ERROR) {
                throw new DmtxException("Error while encoding.");
            } else if ((status > 0) || (result == IntPtr.Zero)) {
                throw new DmtxException("Unknown error.");
            }

            DmtxEncoded ret;
            EncodedInternal intResult = null;
            try {
                intResult = (EncodedInternal)Marshal.PtrToStructure(result, typeof(EncodedInternal));
                ret = new DmtxEncoded();
                ret.SymbolInfo = intResult.SymbolInfo;
                ret.Bitmap = new Bitmap((int)intResult.Width, (int)intResult.Height, PixelFormat.Format24bppRgb);
                Rectangle rect = new Rectangle(0, 0, ret.Bitmap.Width, ret.Bitmap.Height);
                BitmapData bd = ret.Bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
                try {
                    DmtxCopyEncodeResult(intResult.Data, (UInt32)Math.Abs(bd.Stride), bd.Scan0);
                } finally {
                    ret.Bitmap.UnlockBits(bd);
                }
            } catch (Exception ex) {
                throw new DmtxException("Error parsing encode result.", ex);
            } finally {
                try {
                    if (intResult != null) {
                        DmtxFreeEncodeResult(intResult.Data);
                    }
                } catch (Exception ex) {
                    throw new DmtxException("Error freeing memory.", ex);
                }
            }
            return ret;
        }