Exemple #1
0
        async Task <FileData> GetFileData(Android.Net.Uri p_uri)
        {
            var filePath = IOUtil.GetPath(_context, p_uri);

            if (string.IsNullOrEmpty(filePath))
            {
                filePath = IOUtil.IsMediaStore(p_uri.Scheme) ? p_uri.ToString() : p_uri.Path;
            }
            var fileName = GetFileName(_context, p_uri);

            var    fd  = new FileData(filePath, fileName, (ulong)new File(filePath).Length());
            string ext = fd.Ext;

            // 生成文件描述和缩略图
            if (FileFilter.UwpImage.Contains(ext))
            {
                BitmapFactory.Options options = new BitmapFactory.Options();
                // 只解析图片大小,不加载内容
                options.InJustDecodeBounds = true;
                BitmapFactory.DecodeFile(filePath, options);
                fd.Desc = $"{options.OutWidth} x {options.OutHeight} ({ext.TrimStart('.')})";

                int maxSize = Math.Max(options.OutWidth, options.OutHeight);
                if (maxSize > FileData.ThumbSize)
                {
                    // 直接按缩放比例加载
                    options.InJustDecodeBounds = false;
                    options.InSampleSize       = maxSize / FileData.ThumbSize;
                    // v29 弃用
                    //options.InPurgeable = true;
                    Bitmap bmp = BitmapFactory.DecodeFile(filePath, options);

                    fd.ThumbPath = System.IO.Path.Combine(Kit.CachePath, Kit.NewGuid + "-t.jpg");
                    using (var fs = System.IO.File.Create(fd.ThumbPath))
                    {
                        await bmp.CompressAsync(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, fs);
                    }
                    bmp.Recycle();
                }
            }
            else if (FileFilter.UwpVideo.Contains(ext))
            {
                Android.Media.MediaMetadataRetriever media = new Android.Media.MediaMetadataRetriever();
                try
                {
                    await media.SetDataSourceAsync(filePath);

                    string dur    = media.ExtractMetadata(Android.Media.MetadataKey.Duration);
                    string width  = media.ExtractMetadata(Android.Media.MetadataKey.VideoWidth);
                    string height = media.ExtractMetadata(Android.Media.MetadataKey.VideoHeight);
                    fd.Desc = string.Format("{0:HH:mm:ss} ({1} x {2})", new DateTime(long.Parse(dur) * 10000), width, height);
                }
                catch { }
                finally
                {
                    media.Release();
                }

                // 帧缩略图
                var bmp = await ThumbnailUtils.CreateVideoThumbnailAsync(filePath, ThumbnailKind.MiniKind);

                fd.ThumbPath = System.IO.Path.Combine(Kit.CachePath, Kit.NewGuid + "-t.jpg");
                using (var fs = System.IO.File.Create(fd.ThumbPath))
                {
                    await bmp.CompressAsync(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, fs);
                }
                bmp.Recycle();
            }
            else if (FileFilter.UwpAudio.Contains(ext))
            {
                Android.Media.MediaMetadataRetriever media = new Android.Media.MediaMetadataRetriever();
                try
                {
                    await media.SetDataSourceAsync(filePath);

                    string dur = media.ExtractMetadata(Android.Media.MetadataKey.Duration);
                    fd.Desc = string.Format("{0:mm:ss}", new DateTime(long.Parse(dur) * 10000));
                }
                catch { }
                finally
                {
                    media.Release();
                }
            }
            return(fd);
        }
        private void createImages(HorizontalScrollView view)
        {
            MediaMetadataRetriever data = new MediaMetadataRetriever();
            data.SetDataSource(filename);
            int videoLength = Convert.ToInt32(data.ExtractMetadata(MetadataKey.Duration));
            #if DEBUG
            Console.WriteLine("Duration = {0}, videoLength = {1}", data.ExtractMetadata(MetadataKey.Duration), videoLength);
            #endif
            LinearLayout linLay = new LinearLayout(context);
            linLay.Orientation = Android.Widget.Orientation.Horizontal;
            linLay.LayoutParameters = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.FillParent);

            ShowLightboxDialog(Application.Resources.GetString(Resource.String.videoEditGenerateThumbs));
            ImageView imageView = new ImageView(context);
            imageView.LayoutParameters = new ViewGroup.LayoutParams((int)ImageHelper.convertDpToPixel(55f, context),
                                                                    (int)ImageHelper.convertDpToPixel(55f, context));

            #if DEBUG
            for (int n = 0; n < videoLength; ++n)
            {
                using (Bitmap bmp = data.GetFrameAtTime((long)n * 1000, (int)Option.ClosestSync))
                {
                    if (bmp != null)
                        Console.WriteLine("frame {0} contains an image", n);
                }
            }
            #endif

            for (int n = 10; n < /*videoLength*/timeUsed * fps; ++n)
            {
                using (Bitmap bmp = data.GetFrameAtTime((long)n, (int)Option.ClosestSync))
                {
                    if (bmp != null)
                    {
                        using (Bitmap smBmp = Bitmap.CreateScaledBitmap(bmp, (int)ImageHelper.convertDpToPixel(55f, context),
                                                                    (int)ImageHelper.convertDpToPixel(55f, context), true))
                        {
                            int m = new int();
                            m = n;
                            imageView.Tag = m;
                            imageView.Click += (object sender, EventArgs e) => frameClicked(sender, e);
                            imageView.SetImageBitmap(smBmp);
                        }
                    } else
                        imageView.SetBackgroundColor(Color.AliceBlue);
                }
                RunOnUiThread(() => linLay.AddView(imageView));
            }

            DismissLightboxDialog();
            data.Release();
        }
Exemple #3
0
        protected override async void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (resultCode != Result.Ok)
            {
                if (!string.IsNullOrEmpty(_path) && File.Exists(_path))
                {
                    try
                    {
                        File.Delete(_path);
                    }
                    catch { }
                }
                OnCaptured(null);
                Finish();
                return;
            }

            try
            {
                FileData fd  = new FileData(_path, System.IO.Path.GetFileName(_path), (ulong)new Java.IO.File(_path).Length());
                string   ext = fd.Ext;

                // 生成文件描述和缩略图
                if (ext == ".jpg")
                {
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    // 只解析图片大小,不加载内容
                    options.InJustDecodeBounds = true;
                    BitmapFactory.DecodeFile(_path, options);
                    fd.Desc = $"{options.OutWidth} x {options.OutHeight} ({ext.TrimStart('.')})";

                    int maxSize = Math.Max(options.OutWidth, options.OutHeight);
                    if (maxSize > FileData.ThumbSize)
                    {
                        // 直接按缩放比例加载
                        options.InJustDecodeBounds = false;
                        options.InSampleSize       = maxSize / FileData.ThumbSize;
                        // v29 弃用
                        //options.InPurgeable = true;
                        Bitmap bmp = BitmapFactory.DecodeFile(_path, options);

                        fd.ThumbPath = System.IO.Path.Combine(Kit.CachePath, Kit.NewGuid + "-t.jpg");
                        using (var fs = System.IO.File.Create(fd.ThumbPath))
                        {
                            await bmp.CompressAsync(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, fs);
                        }
                        bmp.Recycle();
                    }
                }
                else if (ext == ".mp4")
                {
                    Android.Media.MediaMetadataRetriever media = new Android.Media.MediaMetadataRetriever();
                    try
                    {
                        await media.SetDataSourceAsync(_path);

                        string dur    = media.ExtractMetadata(Android.Media.MetadataKey.Duration);
                        string width  = media.ExtractMetadata(Android.Media.MetadataKey.VideoWidth);
                        string height = media.ExtractMetadata(Android.Media.MetadataKey.VideoHeight);
                        fd.Desc = string.Format("{0:HH:mm:ss} ({1} x {2})", new DateTime(long.Parse(dur) * 10000), width, height);
                    }
                    catch { }
                    finally
                    {
                        media.Release();
                    }

                    // 帧缩略图
                    var bmp = await ThumbnailUtils.CreateVideoThumbnailAsync(_path, ThumbnailKind.MiniKind);

                    fd.ThumbPath = System.IO.Path.Combine(Kit.CachePath, Kit.NewGuid + "-t.jpg");
                    using (var fs = System.IO.File.Create(fd.ThumbPath))
                    {
                        await bmp.CompressAsync(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, fs);
                    }
                    bmp.Recycle();
                }
                OnCaptured(fd);
            }
            catch
            {
                OnCaptured(null);
            }
            finally
            {
                Finish();
            }
        }