Example #1
0
        public virtual Stream Create(BarCodeCreateConfiguration cfg)
        {
            var writer = new BarcodeWriter {
                Format  = (BarcodeFormat)Enum.Parse(typeof(BarcodeFormat), cfg.Format.ToString()),
                Encoder = new MultiFormatWriter(),
                Options = new EncodingOptions {
                    Height      = cfg.Height,
                    Margin      = cfg.Margin,
                    Width       = cfg.Height,
                    PureBarcode = cfg.PureBarcode
                }
            };

#if __IOS__
            return((cfg.ImageType == ImageType.Png)
                                ? writer.Write(cfg.BarCode).AsPNG().AsStream()
                                : writer.Write(cfg.BarCode).AsJPEG().AsStream());
#elif __ANDROID__
            MemoryStream stream = null;

            var cf = cfg.ImageType == ImageType.Png
                                ? Bitmap.CompressFormat.Png
                                : Bitmap.CompressFormat.Jpeg;

            using (var bitmap = writer.Write(cfg.BarCode)) {
//				bitmap.Compress(cf, 0, ms); doesn't work
                var buffer = ByteBuffer.Allocate(bitmap.RowBytes * bitmap.Height);
                bitmap.CopyPixelsToBuffer(buffer);
                buffer.Rewind();
//				var bytes = buffer.ToArray<byte>(); doesn't work
                var classHandle  = JNIEnv.FindClass("java/nio/ByteBuffer");
                var methodId     = JNIEnv.GetMethodID(classHandle, "array", "()[B");
                var resultHandle = JNIEnv.CallObjectMethod(buffer.Handle, methodId);
                var bytes        = JNIEnv.GetArray <byte>(resultHandle);
                JNIEnv.DeleteLocalRef(resultHandle);

                stream = new MemoryStream(bytes);
            }

            stream.Position = 0;
            return(stream);
#elif WINDOWS_PHONE
            return(new MemoryStream(writer.Write(cfg.BarCode).ToByteArray()));
#endif
        }
		public virtual Stream Create(BarCodeCreateConfiguration cfg) {
            var writer = new BarcodeWriter {
				Format = (BarcodeFormat)Enum.Parse(typeof(BarcodeFormat), cfg.Format.ToString()),
                Encoder = new MultiFormatWriter(),
                Options = new EncodingOptions {
					Height = cfg.Height,
					Margin = cfg.Margin,
					Width = cfg.Height,
					PureBarcode = cfg.PureBarcode
                }
            };
#if __IOS__
			return (cfg.ImageType == ImageType.Png)
				? writer.Write(cfg.BarCode).AsPNG().AsStream()
				: writer.Write(cfg.BarCode).AsJPEG().AsStream();
#elif __ANDROID__
			MemoryStream stream = null;

			var cf = cfg.ImageType == ImageType.Png
				? Bitmap.CompressFormat.Png
				: Bitmap.CompressFormat.Jpeg;

			using (var bitmap = writer.Write(cfg.BarCode)) {
//				bitmap.Compress(cf, 0, ms); doesn't work
				var buffer = ByteBuffer.Allocate(bitmap.RowBytes * bitmap.Height);
				bitmap.CopyPixelsToBuffer(buffer);
				buffer.Rewind();
//				var bytes = buffer.ToArray<byte>(); doesn't work
				var classHandle = JNIEnv.FindClass("java/nio/ByteBuffer");
				var methodId = JNIEnv.GetMethodID(classHandle, "array", "()[B");
				var resultHandle = JNIEnv.CallObjectMethod(buffer.Handle, methodId);
				var bytes = JNIEnv.GetArray<byte>(resultHandle);
				JNIEnv.DeleteLocalRef(resultHandle);

				stream = new MemoryStream(bytes);
			}

			stream.Position = 0;
            return stream;
#elif WINDOWS_PHONE
            return new MemoryStream(writer.Write(cfg.BarCode).ToByteArray());
#endif
        }