Example #1
1
        public void OnPreviewFrame(byte[] data, Android.Hardware.Camera camera)
        {
            var paras       = camera.GetParameters();
            var imageformat = paras.PreviewFormat;

            if (imageformat == Android.Graphics.ImageFormatType.Nv21)
            {
                Android.Graphics.YuvImage img = new Android.Graphics.YuvImage(data,
                                                                              imageformat, paras.PreviewSize.Width, paras.PreviewSize.Height, null);

                if (time <= DateTime.Now)
                {
                    using (System.IO.MemoryStream outStream = new System.IO.MemoryStream())
                    {
                        bool didIt = img.CompressToJpeg(new Rect(0, 0, paras.PreviewSize.Width, paras.PreviewSize.Height), 75, outStream);
                        outStream.Seek(0, System.IO.SeekOrigin.Begin);
                        //     Bitmap newBM = BitmapFactory.DecodeStream(outStream);

                        NetProcess.SendRoomMessage(outStream.ToArray());
                        time = DateTime.Now.AddMilliseconds(300);
                    }
                }
            }
        }
        private byte[] ConvertYuvToJpeg (byte[] yuvData, Android.Hardware.Camera camera)
        {
            var cameraParameters = camera.GetParameters ();
            var width = cameraParameters.PreviewSize.Width;
            var height = cameraParameters.PreviewSize.Height;
            var yuv = new YuvImage (yuvData, cameraParameters.PreviewFormat, width, height, null);   
            var ms = new MemoryStream ();
            var quality = 80;   // adjust this as needed
            yuv.CompressToJpeg (new Rect (0, 0, width, height), quality, ms);
            var jpegData = ms.ToArray ();

            return jpegData;
        }
Example #3
0
            public void OnPreviewFrame(byte[] data, Camera camera)
            {
                using (var ms = new MemoryStream())
                {
                    Graph.YuvImage yuvImage = new Graph.YuvImage(data, Graph.ImageFormatType.Nv21, 640, 480, null);
                    yuvImage.CompressToJpeg(new Graph.Rect(0, 0, 640, 480), 100, ms);
                    var msArray       = ms.ToArray();
                    var splittedArray = this.SplitToSublists(msArray.ToList());

                    foreach (var item in splittedArray)
                    {
                        this.sender.Send(new MultiPlatform.Packets.Video.Nv21FormatVideoPacket()
                        {
                            IsPart = true, Data = item.ToArray()
                        });
                    }
                }
            }
Example #4
0
        public void OnPreviewFrame(byte [] bytes, Android.Hardware.Camera camera)
        {
            if (!isAnalyzing)
                return;

            //Check and see if we're still processing a previous frame
            if (processingTask != null && !processingTask.IsCompleted)
                return;

            if ((DateTime.UtcNow - lastPreviewAnalysis).TotalMilliseconds < options.DelayBetweenAnalyzingFrames)
                return;

            // Delay a minimum between scans
            if (wasScanned && ((DateTime.UtcNow - lastPreviewAnalysis).TotalMilliseconds < options.DelayBetweenContinuousScans))
                return;

            wasScanned = false;

            var cameraParameters = camera.GetParameters();
            var width = cameraParameters.PreviewSize.Width;
            var height = cameraParameters.PreviewSize.Height;
            //var img = new YuvImage(bytes, ImageFormatType.Nv21, cameraParameters.PreviewSize.Width, cameraParameters.PreviewSize.Height, null);
            lastPreviewAnalysis = DateTime.UtcNow;

            processingTask = Task.Factory.StartNew (() =>
            {
                try
                {

                    if (barcodeReader == null)
                    {
                        barcodeReader = new BarcodeReader (null, null, null, (p, w, h, f) =>
                                                      new PlanarYUVLuminanceSource (p, w, h, 0, 0, w, h, false));
                        //new PlanarYUVLuminanceSource(p, w, h, dataRect.Left, dataRect.Top, dataRect.Width(), dataRect.Height(), false))

                        if (this.options.TryHarder.HasValue)
                            barcodeReader.Options.TryHarder = this.options.TryHarder.Value;
                        if (this.options.PureBarcode.HasValue)
                            barcodeReader.Options.PureBarcode = this.options.PureBarcode.Value;
                        if (!string.IsNullOrEmpty (this.options.CharacterSet))
                            barcodeReader.Options.CharacterSet = this.options.CharacterSet;
                        if (this.options.TryInverted.HasValue)
                            barcodeReader.TryInverted = this.options.TryInverted.Value;

                        if (this.options.PossibleFormats != null && this.options.PossibleFormats.Count > 0)
                        {
                            barcodeReader.Options.PossibleFormats = new List<BarcodeFormat> ();

                            foreach (var pf in this.options.PossibleFormats)
                                barcodeReader.Options.PossibleFormats.Add (pf);
                        }
                    }

                    bool rotate = false;
                    int newWidth = width;
                    int newHeight = height;

                    var cDegrees = getCameraDisplayOrientation(this.activity);

                    if (cDegrees == 90 || cDegrees == 270)
                    {
                        rotate = true;
                        newWidth = height;
                        newHeight = width;
                    }

                    if (rotate)
                            bytes = rotateYUV420Degree90(bytes, width, height);//rotateCounterClockwise(bytes, width, height);

                    var result = barcodeReader.Decode (bytes, newWidth, newHeight, RGBLuminanceSource.BitmapFormat.Unknown);

                    if (result == null || string.IsNullOrEmpty (result.Text))
                        return;

                    Android.Util.Log.Debug (MobileBarcodeScanner.TAG, "Barcode Found: " + result.Text);

                        var img = new YuvImage(bytes, cameraParameters.PreviewFormat, newWidth, newHeight, null);

                    System.IO.MemoryStream outStream = new System.IO.MemoryStream();
                    bool didIt = img.CompressToJpeg(new Rect(0,0,newWidth, newHeight), 75, outStream);
                    outStream.Seek(0, System.IO.SeekOrigin.Begin);
                    Bitmap newBM = BitmapFactory.DecodeStream(outStream);

                    result.CaptureImage = newBM;

                    wasScanned = true;
                    callback (result);
                }
                catch (ReaderException)
                {
                    Android.Util.Log.Debug (MobileBarcodeScanner.TAG, "No barcode Found");
                    // ignore this exception; it happens every time there is a failed scan

                }
                catch (Exception)
                {
                    // TODO: this one is unexpected.. log or otherwise handle it
                    throw;
                }

            });
        }
		public void OnPreviewFrame (byte [] bytes, Android.Hardware.Camera camera)
		{
			if ((DateTime.Now - lastPreviewAnalysis).TotalMilliseconds < options.DelayBetweenAnalyzingFrames)
				return;
			
			try 
			{
				var cameraParameters = camera.GetParameters();
				var img = new YuvImage(bytes, ImageFormatType.Nv21, cameraParameters.PreviewSize.Width, cameraParameters.PreviewSize.Height, null);	
				
				if (barcodeReader == null)
				{
					barcodeReader = new BarcodeReader(null, null, null, (p, w, h, f) => 
					                                  new PlanarYUVLuminanceSource(p, w, h, 0, 0, w, h, false));
					//new PlanarYUVLuminanceSource(p, w, h, dataRect.Left, dataRect.Top, dataRect.Width(), dataRect.Height(), false))
					
					if (this.options.TryHarder.HasValue)
						barcodeReader.Options.TryHarder = this.options.TryHarder.Value;
					if (this.options.PureBarcode.HasValue)
						barcodeReader.Options.PureBarcode = this.options.PureBarcode.Value;
					if (!string.IsNullOrEmpty (this.options.CharacterSet))
						barcodeReader.Options.CharacterSet = this.options.CharacterSet;
					if (this.options.TryInverted.HasValue)
						barcodeReader.TryInverted = this.options.TryInverted.Value;
					
					if (this.options.PossibleFormats != null && this.options.PossibleFormats.Count > 0)
					{
						barcodeReader.Options.PossibleFormats = new List<BarcodeFormat>();
						
						foreach (var pf in this.options.PossibleFormats)
							barcodeReader.Options.PossibleFormats.Add(pf);
					}
					
					//Always autorotate on android
					barcodeReader.AutoRotate = true;
				}
				
				//Try and decode the result
				var result = barcodeReader.Decode(img.GetYuvData(), img.Width, img.Height, RGBLuminanceSource.BitmapFormat.Unknown);
				
				lastPreviewAnalysis = DateTime.Now;
				
				if (result == null || string.IsNullOrEmpty (result.Text))
					return;
				
				Android.Util.Log.Debug("ZXing.Mobile", "Barcode Found: " + result.Text);
				
				ShutdownCamera();

				callback(result);

			} catch (ReaderException) {
				Android.Util.Log.Debug("ZXing.Mobile", "No barcode Found");
				// ignore this exception; it happens every time there is a failed scan
				
			} catch (Exception) {
				// TODO: this one is unexpected.. log or otherwise handle it
				throw;
			}
		}
Example #6
0
        public void OnPreviewFrame(byte[] data, Android.Hardware.Camera camera)
        {
            var paras       = camera.GetParameters();
            var imageformat = paras.PreviewFormat;

            switch (imageformat)
            {
            case ImageFormatType.Nv16:
            case ImageFormatType.Nv21:
            case ImageFormatType.Yuy2:
            case ImageFormatType.Yv12:
            {
                //Android.Graphics.YuvImage img = new Android.Graphics.YuvImage(data,
                //                               imageformat, paras.PreviewSize.Width, paras.PreviewSize.Height, null);

                //System.IO.MemoryStream outStream = new System.IO.MemoryStream();

                //img.CompressToJpeg(new Rect(0, 0, paras.PreviewSize.Width, paras.PreviewSize.Height), 30, outStream);

                //Frames.Enqueue(outStream);

                //if (checktime < DateTime.Now)
                //{
                //    NetProcess.SendRoomBITMAPMessage(Frames);
                //    checktime = DateTime.Now.AddMilliseconds(50);
                //    Frames.Clear();
                //}

                if (checktime < DateTime.Now)
                {
                    Android.Graphics.YuvImage img = new Android.Graphics.YuvImage(data,
                                                                                  imageformat, paras.PreviewSize.Width, paras.PreviewSize.Height, null);

                    System.IO.MemoryStream outStream = new System.IO.MemoryStream();

                    img.CompressToJpeg(new Rect(0, 0, paras.PreviewSize.Width, paras.PreviewSize.Height), 30, outStream);

                    Frames.Enqueue(outStream);

                    //서버쪽은 임시 주석
                    //server.ImagesSource.Enqueue(outStream);

                    if (Frames.Count > 5)
                    {
                        NetProcess.SendRoomBITMAPMessage(Frames);

                        Frames.Clear();
                    }

                    checktime = DateTime.Now.AddMilliseconds(100);
                }
            }
            break;

            case ImageFormatType.Jpeg:

                Frames.Enqueue(new System.IO.MemoryStream(data));

                if (checktime < DateTime.Now)
                {
                    foreach (var frame in Frames)
                    {
                        server.ImagesSource.Enqueue(frame);
                    }

                    Frames.Clear();
                }


                break;
            }
        }
		public void OnPreviewFrame (byte [] bytes, Android.Hardware.Camera camera)
		{
			if ((DateTime.Now - lastPreviewAnalysis).TotalMilliseconds < 150)
				return;
			
			try 
			{
				//Fix for image not rotating on devices
				byte[] rotatedData = new byte[bytes.Length];
				for (int y = 0; y < height; y++) {
				    for (int x = 0; x < width; x++)
				        rotatedData[x * height + height - y - 1] = bytes[x + y * width];
				}
				
				var cameraParameters = camera.GetParameters();

				//Changed to using a YUV Image to get the byte data instead of manually working with it!
				var img = new YuvImage(rotatedData, ImageFormatType.Nv21, cameraParameters.PreviewSize.Width, cameraParameters.PreviewSize.Height, null);	
				var dataRect = GetFramingRectInPreview();
			
				var luminance = new PlanarYUVLuminanceSource (img.GetYuvData(), width, height, dataRect.Left, dataRect.Top, dataRect.Width(), dataRect.Height(), false);
				//var luminance = new PlanarYUVLuminanceSource(img.GetYuvData(), cameraParameters.PreviewSize.Width, cameraParameters.PreviewSize.Height, 0, 0, cameraParameters.PreviewSize.Width, cameraParameters.PreviewSize.Height, false);
				var binarized = new BinaryBitmap (new ZXing.Common.HybridBinarizer(luminance));
				var result = reader.decodeWithState(binarized);

				lastPreviewAnalysis = DateTime.Now;

				if (result == null || string.IsNullOrEmpty (result.Text))
					return;

				Android.Util.Log.Debug("ZXing.Mobile", "Barcode Found: " + result.Text);

				ShutdownCamera();

				activity.OnScan (result);

			} catch (ReaderException) {
				Android.Util.Log.Debug("ZXing.Mobile", "No barcode Found");
				// ignore this exception; it happens every time there is a failed scan

			} catch (Exception){

				// TODO: this one is unexpected.. log or otherwise handle it

				throw;
			}
		}
		public void OnPreviewFrame(byte[] bytes, Android.Hardware.Camera camera)
		{
			//Check and see if we're still processing a previous frame
			if(processingTask != null && !processingTask.IsCompleted) return;

			if((DateTime.UtcNow - lastPreviewAnalysis).TotalMilliseconds < options.DelayBetweenAnalyzingFrames) return;

			var cameraParameters = camera.GetParameters();
			var width = cameraParameters.PreviewSize.Width;
			var height = cameraParameters.PreviewSize.Height;
			var img = new YuvImage(bytes,
			                       ImageFormatType.Nv21,
			                       cameraParameters.PreviewSize.Width,
			                       cameraParameters.PreviewSize.Height,
			                       null);	

			lastPreviewAnalysis = DateTime.UtcNow;

			processingTask = Task.Factory.StartNew(() =>{
				try
				{

					if(barcodeReader == null)
					{
						barcodeReader = new BarcodeReader(null, null, null, (p, w, h, f) => 
					                                  new PlanarYUVLuminanceSource(p, w, h, 0, 0, w, h, false));
						//new PlanarYUVLuminanceSource(p, w, h, dataRect.Left, dataRect.Top, dataRect.Width(), dataRect.Height(), false))
					
						if(this.options.TryHarder.HasValue) barcodeReader.Options.TryHarder = this.options.TryHarder.Value;
						if(this.options.PureBarcode.HasValue) barcodeReader.Options.PureBarcode = this.options.PureBarcode.Value;
						if(!string.IsNullOrEmpty(this.options.CharacterSet)) barcodeReader.Options.CharacterSet = this.options.CharacterSet;
						if(this.options.TryInverted.HasValue) barcodeReader.TryInverted = this.options.TryInverted.Value;
					
						if(this.options.PossibleFormats != null && this.options.PossibleFormats.Count > 0)
						{
							barcodeReader.Options.PossibleFormats = new List<BarcodeFormat>();
						
							foreach(var pf in this.options.PossibleFormats) barcodeReader.Options.PossibleFormats.Add(pf);
						}
					}

					bool rotate = false;
					int newWidth = width;
					int newHeight = height;

					var cDegrees = getCameraDisplayOrientation(this.activity);

					if(cDegrees == 90 || cDegrees == 270)
					{
						rotate = true;
						newWidth = height;
						newHeight = width;
					}
					
					var start = PerformanceCounter.Start();
					
					if(rotate) bytes = rotateCounterClockwise(bytes, width, height);
										
					var result = barcodeReader.Decode(bytes, newWidth, newHeight, RGBLuminanceSource.BitmapFormat.Unknown);

					PerformanceCounter.Stop(start,
					                        "Decode Time: {0} ms (width: " + width + ", height: " + height + ", degrees: " + cDegrees + ", rotate: " + rotate + ")");
				
					if(result == null || string.IsNullOrEmpty(result.Text)) return;
				
					Android.Util.Log.Debug("ZXing.Mobile", "Barcode Found: " + result.Text);
					var mStream = new MemoryStream();
					img.CompressToJpeg(new Rect(0, 0, width, height), 100, mStream);
					byte[] jpegBytes = mStream.ToArray();
					result.Image = jpegBytes;
					ShutdownCamera();

					callback(result);

				} catch(ReaderException)
				{
					Android.Util.Log.Debug("ZXing.Mobile", "No barcode Found");
					// ignore this exception; it happens every time there is a failed scan
				
				} catch(Exception)
				{
					// TODO: this one is unexpected.. log or otherwise handle it
					throw;
				}

			});
		}
Example #9
0
		public void OnPreviewFrame (byte [] bytes, Android.Hardware.Camera camera)
		{
			if ((DateTime.Now - lastPreviewAnalysis).TotalMilliseconds < options.DelayBetweenAnalyzingFrames)
				return;
			
			try 
			{
				/* OLD Android Code
				//Fix for image not rotating on devices
				byte[] rotatedData = new byte[bytes.Length];
				for (int y = 0; y < height; y++) {
				    for (int x = 0; x < width; x++)
				        rotatedData[x * height + height - y - 1] = bytes[x + y * width];
				}
				
				var cameraParameters = camera.GetParameters();

				//Changed to using a YUV Image to get the byte data instead of manually working with it!
				var img = new YuvImage(rotatedData, ImageFormatType.Nv21, cameraParameters.PreviewSize.Width, cameraParameters.PreviewSize.Height, null);	
				var dataRect = GetFramingRectInPreview();
			
				var luminance = new PlanarYUVLuminanceSource (img.GetYuvData(), width, height, dataRect.Left, dataRect.Top, dataRect.Width(), dataRect.Height(), false);
				//var luminance = new PlanarYUVLuminanceSource(img.GetYuvData(), cameraParameters.PreviewSize.Width, cameraParameters.PreviewSize.Height, 0, 0, cameraParameters.PreviewSize.Width, cameraParameters.PreviewSize.Height, false);
				var binarized = new BinaryBitmap (new ZXing.Common.HybridBinarizer(luminance));
				var result = reader.decodeWithState(binarized);
				*/
				
				
				
				var cameraParameters = camera.GetParameters();
				var img = new YuvImage(bytes, ImageFormatType.Nv21, cameraParameters.PreviewSize.Width, cameraParameters.PreviewSize.Height, null);	
				var dataRect = GetFramingRectInPreview();
				
				//var barcodeReader = new BarcodeReader(null, p => new PlanarYUVLuminanceSource(img.GetYuvData(), img.Width, img.Height, dataRect.Left, dataRect.Top,
				//                                            dataRect.Width(), dataRect.Height(), false), null, null)
				//{
				//	AutoRotate = true,
				//	TryHarder = true,
				//};

				var barcodeReader = new BarcodeReader(null, null, null, (p, w, h, f) => 
				    new PlanarYUVLuminanceSource(p, w, h, 0, 0, w, h, false))
					//new PlanarYUVLuminanceSource(p, w, h, dataRect.Left, dataRect.Top, dataRect.Width(), dataRect.Height(), false))
				{
					AutoRotate = true,
					TryHarder = false
				};

				if (this.options.PureBarcode.HasValue && this.options.PureBarcode.Value)
					barcodeReader.PureBarcode = this.options.PureBarcode.Value;

				if (this.options.PossibleFormats != null && this.options.PossibleFormats.Count > 0)
					barcodeReader.PossibleFormats = this.options.PossibleFormats;

				var result = barcodeReader.Decode(img.GetYuvData(), img.Width, img.Height, RGBLuminanceSource.BitmapFormat.Unknown);


				lastPreviewAnalysis = DateTime.Now;

				if (result == null || string.IsNullOrEmpty (result.Text))
					return;

				Android.Util.Log.Debug("ZXing.Mobile", "Barcode Found: " + result.Text);

				ShutdownCamera();

				activity.OnScan (result);

			} catch (ReaderException) {
				Android.Util.Log.Debug("ZXing.Mobile", "No barcode Found");
				// ignore this exception; it happens every time there is a failed scan

			} catch (Exception){

				// TODO: this one is unexpected.. log or otherwise handle it

				throw;
			}
		}
Example #10
0
        public void OnPreviewFrame(byte[] data, Android.Hardware.Camera camera)
        {
            var paras       = camera.GetParameters();
            var imageformat = paras.PreviewFormat;

            switch (imageformat)
            {
            case ImageFormatType.Nv16:
            case ImageFormatType.Nv21:
            case ImageFormatType.Yuy2:
            case ImageFormatType.Yv12:
            {
                if (checktime < DateTime.Now)
                {
                    Android.Graphics.YuvImage img = new Android.Graphics.YuvImage(data,
                                                                                  imageformat, paras.PreviewSize.Width, paras.PreviewSize.Height, null);

                    System.IO.MemoryStream outStream = new System.IO.MemoryStream();

                    img.CompressToJpeg(new Rect(0, 0, paras.PreviewSize.Width, paras.PreviewSize.Height), 50, outStream);

                    var frameToStream = outStream.ToArray();
                    var bitmap        = BitmapFactory.DecodeByteArray(frameToStream, 0, frameToStream.Length);

                    outStream = null;

                    outStream = new System.IO.MemoryStream();

                    bitmap = Bitmap.CreateScaledBitmap(bitmap, 320, 240, true);

                    var mat = new Matrix();

                    if (renderer.currentFacing == Android.Hardware.CameraFacing.Front)
                    {
                        mat.PostRotate(-90);
                    }
                    else
                    {
                        mat.PostRotate(90);
                    }

                    bitmap = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, mat, true);

                    bitmap.Compress(Bitmap.CompressFormat.Jpeg, 50, outStream);



                    Frames.Enqueue(outStream);

                    //서버쪽은 임시 주석
                    //render.server.ImagesSource.Enqueue(outStream);

                    if (Frames.Count > 0)
                    {
                        total_bytes_sent += outStream.Length;
                        count_sent       += 1;
                        NetProcess.SendRoomBITMAPMessage(Frames);

                        Frames.Clear();
                    }

                    checktime = DateTime.Now.AddMilliseconds(33);
                }
            }
            break;

            case ImageFormatType.Jpeg:

                Frames.Enqueue(new System.IO.MemoryStream(data));

                if (checktime < DateTime.Now)
                {
                    foreach (var frame in Frames)
                    {
                        renderer.server.ImagesSource.Enqueue(frame);
                    }

                    Frames.Clear();

                    checktime = DateTime.Now.AddMilliseconds(33);
                }
                break;
            }
        }