| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 | using System;using System.Collections.Generic;using System.Drawing;using System.IO;using Android.Content;using Android.Graphics;using Android.Media;using Java.IO;using Bitmap = Android.Graphics.Bitmap;using File = System.IO.File;using Path = System.IO.Path;[assembly: Xamarin.Forms.Dependency(typeof(InABox.Mobile.Android.ImageToolsDroid))]namespace InABox.Mobile.Android{    public class ImageToolsDroid: IImageTools    {        public byte[] CreateVideoThumbnail(byte[] video, float maxwidth, float maxheight)        {            byte[] result = null;            var filename = Path.Combine(                Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),                $"{Guid.NewGuid().ToString()}.tmp"            );            File.WriteAllBytes(filename,video);            using (FileInputStream fs = new FileInputStream(filename))            {                FileDescriptor fd = fs.FD;                MediaMetadataRetriever retriever = new MediaMetadataRetriever();                retriever.SetDataSource(fd);                Bitmap bitmap = retriever.GetFrameAtTime(0);                if (bitmap != null)                {                    MemoryStream stream = new MemoryStream();                    bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);                    result = stream.ToArray();                }            }            File.Delete(filename);            if (result != null)                result = CreateThumbnail(result, maxwidth, maxheight);            return result;        }                public byte[] CreateThumbnail(byte[] source, float maxwidth, float maxheight)        {            byte[] result = { };            using (var image = BitmapFactory.DecodeByteArray(source, 0, source.Length))            {                  if (image != null)                {                    var size = new Size((int)image.GetBitmapInfo().Height, (int)image.GetBitmapInfo().Width);                    if ((size.Width > 0) && (size.Height > 0))                    {                        var maxFactor = Math.Min(maxwidth / size.Width, maxheight / size.Height);                        var width = maxFactor * size.Width;                        var height = maxFactor * size.Height;                        using (var tgt = Bitmap.CreateScaledBitmap(image, (int)height, (int)width, true))                        {                            if (tgt != null)                            {                                using (var ms = new MemoryStream())                                {                                    tgt.Compress(Bitmap.CompressFormat.Jpeg, 95, ms);                                    result = ms.ToArray();                                }                                tgt.Recycle();                            }                        }                    }                    image.Recycle();                }            }            return result;        }    }}
 |