Ejemplo n.º 1
0
        public void ProcessImage(Bitmap bitmap, Action <string, string> resultAction)
        {
            try
            {
                var app = FirebaseApp.InitializeApp(Application.Context);
                FirebaseVisionTextRecognizer detector = FirebaseVision.GetInstance(app).OnDeviceTextRecognizer;


                FirebaseVisionCloudDocumentRecognizerOptions options = new FirebaseVisionCloudDocumentRecognizerOptions.Builder()
                                                                       .SetLanguageHints(new List <String> {
                    "en", "ru"
                })
                                                                       .Build();
                FirebaseVisionDocumentTextRecognizer det = FirebaseVision.GetInstance(app).GetCloudDocumentTextRecognizer(options);

                FirebaseVisionImage image = FirebaseVisionImage.FromBitmap(bitmap);
                var result = det
                             .ProcessImage(image)
                             .AddOnCompleteListener(new ImageDetectionListener(resultAction, activity));
            }
            catch (Exception)
            {
                resultAction?.Invoke(null, activity.GetString(Resource.String.unexpectedException));
            }
        }
        /// <summary>
        /// OnImageAvailableから呼び出される
        /// </summary>
        private async void ProcessOnImageAvailable()
        {
            //this._owner.BackgroundHandler.Post(new ImageSaver(reader.AcquireNextImage()));
            var        image  = this._owner.ImageReader.AcquireNextImage();
            ByteBuffer buffer = image.GetPlanes()[0].Buffer;

            byte[] jpegBytes = new byte[buffer.Remaining()];
            buffer.Get(jpegBytes);
            image.Close();

            //acquireNextImageのあとでないと,次を取得せずに止まる.
            if (DroidCameraPreview2.UPDATE_FRAME_SPAN == 0)
            {
                return;
            }

            //Frameカウントを取得できていないとずっと動き続けてしまうので
            if (this._owner.FrameCount == 0)
            {
                return;
            }

            //指定したフレーム間隔でBitmapを取得する
            if (this._owner.FrameCount % DroidCameraPreview2.UPDATE_FRAME_SPAN != 0)
            {
                return;
            }

            //bytes[]→回転+bitmap→byte[]
            using (var ms = new MemoryStream(jpegBytes))
            {
                #region 画像回転
                this._owner.AndroidBitmap_Rotated = await ImageUtility.RotateAndBitmap(ms, this._owner.SensorOrientation);

                #endregion

                #region byte[]へ
                //AndroidBitmap→byte[]
                this._owner.JpegBytes = await ImageUtility.AndroidBitmapToByteArray(this._owner.AndroidBitmap_Rotated);

                #endregion
            }

            return;

            #region Firebase face detectを使った顔検出
            var imgForFirebase = FirebaseVisionImage.FromBitmap(this._owner.AndroidBitmap_Rotated);
            var options        = new FirebaseVisionFaceDetectorOptions.Builder().Build();
            var detector       = FirebaseVision.GetInstance(this._owner.FirebaseApp)
                                 .GetVisionFaceDetector(options);

            detector.DetectInImage(imgForFirebase)
            .AddOnSuccessListener(this._owner.DetectSuccessListener);
            #endregion
        }