//http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966


        public static async void SaveSmallBitmapAsJPG()
        {
            //  var path = new File(
            //                Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures), "OCR").AbsolutePath;
            //   var smallfilePath = System.IO.Path.Combine(path, "small.jpg");
            //   var bigfilePath = System.IO.Path.Combine(path, "OCR.jpg");

            BitmapFactory.Options opts = new BitmapFactory.Options();

            // load the image and have BitmapFactory resize it for us.
            opts.InSampleSize       = 4;     //  1/4 size
            opts.InJustDecodeBounds = false; //set to false to get the whole image not just the bounds
            Bitmap bitmap = await BitmapFactory.DecodeFileAsync(AppPath.bigfilePath(), opts);


            //rotate a bmp
            Matrix matrix = new Matrix();

            matrix.PostRotate(90);
            var rotatedBitmap = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix, true);


            //write it back
            using (var stream = new FileStream(AppPath.smallfilePath(), FileMode.Create))
            {
                await rotatedBitmap.CompressAsync(Bitmap.CompressFormat.Jpeg, 70, stream);//70% compressed

                stream.Close();
            }

            //    await stream.FlushAsync();
            // Free the native object associated with this bitmap, and clear the reference to the pixel data. This will not free the pixel data synchronously; it simply allows it to be garbage collected if there are no other references. The bitmap is marked as "dead", meaning it will throw an exception if getPixels() or setPixels() is called, and will draw nothing.
            //  bitmap.Recycle();
            //     GC.Collect();
        }
예제 #2
0
        //the result of the StartActivityForResult
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            // Make it available in the gallery by adding it to the media database

            Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);

            AppPath.OCRfile = new File(AppPath.bigfilePath());
            Uri contentUri = Uri.FromFile(AppPath.OCRfile);

            mediaScanIntent.SetData(contentUri);
            SendBroadcast(mediaScanIntent); //tell everything about the new pic?


            BitmapOperations.SaveSmallBitmapAsJPG();
        }