예제 #1
0
        public void TestDecode()
        {
            Bitmap        bm  = GetBitmapFromResource("Libdmtx.TestImages.Test001.png");
            DecodeOptions opt = new DecodeOptions();

            DmtxDecoded[] decodeResults = Dmtx.Decode(bm, opt);
            Assert.AreEqual(1, decodeResults.Length);
            string data = Encoding.ASCII.GetString(decodeResults[0].Data).TrimEnd('\0');

            Assert.AreEqual("Test", data);
        }
예제 #2
0
        public void TestDecodeWithCallback()
        {
            List <DmtxDecoded> decodeResults = new List <DmtxDecoded>();
            Bitmap             bm            = GetBitmapFromResource("Libdmtx.TestImages.Test002.png");
            DecodeOptions      opt           = new DecodeOptions();

            Dmtx.Decode(bm, opt, d => decodeResults.Add(d));
            Assert.AreEqual(2, decodeResults.Count);
            string data1 = Encoding.ASCII.GetString(decodeResults[0].Data).TrimEnd('\0');

            Assert.AreEqual("Test1", data1);
            string data2 = Encoding.ASCII.GetString(decodeResults[1].Data).TrimEnd('\0');

            Assert.AreEqual("Test2", data2);
        }
예제 #3
0
        public void TestDecodeWithCallbackThrowException()
        {
            Bitmap        bm        = GetBitmapFromResource("Libdmtx.TestImages.Test002.png");
            DecodeOptions opt       = new DecodeOptions();
            int           callCount = 0;

            try {
                Dmtx.Decode(bm, opt, d => {
                    callCount++;
                    throw new Exception("Test Exception");
                });
                Assert.Fail("Should have gotten an exception.");
            } catch (Exception ex) {
                Assert.AreEqual(1, callCount);
                Assert.AreEqual("Test Exception", ex.Message);
            }
        }
예제 #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 + "]");
            }
        }