public byte[] GetVideoThumbnail(string path) { try { Bitmap bmThumbnail = ThumbnailUtils.CreateVideoThumbnail(path, ThumbnailKind.FullScreenKind); byte[] bitmapData; using (var stream = new MemoryStream()) { bmThumbnail.Compress(Bitmap.CompressFormat.Png, 0, stream); bitmapData = stream.ToArray(); } //ByteArrayOutputStream stream = new ByteArrayOutputStream(); //bmThumbnail.Compress(Bitmap.CompressFormat.Png, 100, stream.); //byte[] byteArray = stream.ToByteArray(); return(bitmapData); } catch (Exception e) { //Console.WriteLine(e); } // // MINI_KIND: 512 x 384 thumbnail // bmThumbnail = ThumbnailUtils.createVideoThumbnail(filePath, //MediaStore.Video.Thumbnails.MINI_KIND); // thumbnail_mini.setImageBitmap(bmThumbnail); return(null); }
private Bitmap GetThunbailFromVideo(string filename, ThumbnailKind ScreenKind) { Bitmap thumb = ThumbnailUtils.CreateVideoThumbnail(filename, ScreenKind); return(RotateBitmap(thumb, 90)); }
/** * * 获取视频的缩略图 先通过ThumbnailUtils来创建一个视频的缩略图,然后再利用ThumbnailUtils来生成指定大小的缩略图。 * * 如果想要的缩略图的宽和高都小于MICRO_KIND,则类型要使用MICRO_KIND作为kind的值,这样会节省内存。 * * * * @param videoPath * * 视频的路径 * * @param width * * 指定输出视频缩略图的宽度 * * @param height * * 指定输出视频缩略图的高度度 * * @param kind * * 参照MediaStore.Images.Thumbnails类中的常量MINI_KIND和MICRO_KIND。 * * 其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96 * * @return 指定大小的视频缩略图 * */ public static Bitmap GetVideoThumbnail(String videoPath, int width, int height, ThumbnailKind kind) { Bitmap bitmap = null; bitmap = ThumbnailUtils.CreateVideoThumbnail(videoPath, kind); bitmap = ThumbnailUtils.ExtractThumbnail(bitmap, width, height, ThumnailExtractOptions.RecycleInput); return(bitmap); }
private MediaModel CreateMediaModel(Android.Net.Uri oUri) { MediaModel oModel = null; var oType = CrossCurrentActivity.Current.Activity.ContentResolver.GetType(oUri); var oPath = GetPathUri(oUri); if (oPath != null) { string sPath = string.Empty; string sThumpnailPath = string.Empty; string sFileName = System.IO.Path.GetFileName(oPath); string sExt = System.IO.Path.GetExtension(oPath) ?? string.Empty; MediaFileType mediaFileType = MediaFileType.Image; if (oType.StartsWith(Enum.GetName(typeof(MediaFileType), MediaFileType.Image), StringComparison.CurrentCultureIgnoreCase)) { var fullImage = ImageHelper.RotateImage(oPath, 1); sPath = FileService.GetOutputPath(MediaFileType.Image, TempDir, $"{sFileName}{sExt}"); File.WriteAllBytes(sPath, fullImage); var oThumpnailImg = ImageHelper.RotateImage(oPath, 0.25f); sThumpnailPath = FileService.GetOutputPath(MediaFileType.Image, TempDir, $"{sFileName}-THUMBNAIL{sExt}"); File.WriteAllBytes(sThumpnailPath, oThumpnailImg); } else if (oType.StartsWith(Enum.GetName(typeof(MediaFileType), MediaFileType.Video), StringComparison.CurrentCultureIgnoreCase)) { sPath = oPath; var bitmap = ThumbnailUtils.CreateVideoThumbnail(oPath, ThumbnailKind.MiniKind); sThumpnailPath = FileService.GetOutputPath(MediaFileType.Image, TempDir, $"{sFileName}-THUMBNAIL{sExt}"); var stream = new FileStream(sThumpnailPath, FileMode.Create); bitmap?.Compress(Bitmap.CompressFormat.Jpeg, 100, stream); stream.Close(); mediaFileType = MediaFileType.Video; } if (!string.IsNullOrEmpty(sPath) && !string.IsNullOrEmpty(sThumpnailPath)) { oModel = new MediaModel() { FilePath = sPath, MediaType = mediaFileType, PreviewFile = sThumpnailPath }; } } return(oModel); }
MediaFile CreateMediaFileFromUri(Android.Net.Uri uri) { MediaFile mediaFile = null; var type = CrossCurrentActivity.Current.Activity.ContentResolver.GetType(uri); var path = GetRealPathFromURI(uri); if (path != null) { string fullPath = string.Empty; string thumbnailImagePath = string.Empty; var fileName = System.IO.Path.GetFileNameWithoutExtension(path); var ext = System.IO.Path.GetExtension(path) ?? string.Empty; MediaFileType mediaFileType = MediaFileType.Image; var imageTypeName = Enum.GetName(typeof(MediaFileType), MediaFileType.Image).ToLowerInvariant(); var videoTypeName = Enum.GetName(typeof(MediaFileType), MediaFileType.Video).ToLowerInvariant(); if (type.ToLower().StartsWith(imageTypeName, StringComparison.CurrentCultureIgnoreCase)) { var fullImage = ImageHelpers.RotateImage(path, 1); var thumbImage = ImageHelpers.RotateImage(path, 0.25f); fullPath = FileHelper.GetOutputPath(MediaFileType.Image, TemporalDirectoryName, $"{fileName}{ext}"); File.WriteAllBytes(fullPath, fullImage); thumbnailImagePath = FileHelper.GetOutputPath(MediaFileType.Image, TemporalDirectoryName, $"{fileName}-THUMBNAIL{ext}"); File.WriteAllBytes(thumbnailImagePath, thumbImage); } else if (type.ToLowerInvariant().StartsWith(videoTypeName, StringComparison.CurrentCultureIgnoreCase)) { fullPath = path; var bitmap = ThumbnailUtils.CreateVideoThumbnail(path, ThumbnailKind.MiniKind); thumbnailImagePath = FileHelper.GetOutputPath(MediaFileType.Image, TemporalDirectoryName, $"{fileName}-THUMBNAIL{ext}"); var stream = new FileStream(thumbnailImagePath, FileMode.Create); bitmap?.Compress(Bitmap.CompressFormat.Jpeg, 100, stream); stream.Close(); mediaFileType = MediaFileType.Video; } if (!string.IsNullOrEmpty(fullPath) && !string.IsNullOrEmpty(thumbnailImagePath)) { mediaFile = new MediaFile() { Path = fullPath, Type = mediaFileType, PreviewPath = thumbnailImagePath }; } } return(mediaFile); }
public ImageSource GetVideoThumbnail(string url) { Bitmap thumb = ThumbnailUtils.CreateVideoThumbnail(url, ThumbnailKind.MiniKind); var imgsrc = ImageSource.FromStream(() => { MemoryStream ms = new MemoryStream(); thumb.Compress(Bitmap.CompressFormat.Jpeg, 100, ms); ms.Seek(0L, SeekOrigin.Begin); return(ms); }); return(imgsrc); }
public System.IO.Stream GetThumbnail(string pathMediaPath) { try { Bitmap thumb = ThumbnailUtils.CreateVideoThumbnail(pathMediaPath, ThumbnailKind.FullScreenKind); System.IO.Stream stream = new MemoryStream(); thumb.Compress(Bitmap.CompressFormat.Jpeg, 100, stream); stream.Seek(0L, SeekOrigin.Begin); return(stream); } catch (Exception exc) { Debugger.Break(); return(null); } }
private async Task <string> SaveThumbnailToFileAsync(string path) { return(await Task.Run(() => { string thumbnailPath; var bmThumbnail = ThumbnailUtils.CreateVideoThumbnail(path, ThumbnailKind.MiniKind); if (bmThumbnail != null) { thumbnailPath = SaveToFile(path, bmThumbnail); } else { // TODO : Default Thumbnail Image thumbnailPath = String.Empty; } return thumbnailPath; })); }
public override View GetView(int position, View convertView, ViewGroup parent) { ViewHolder holder; var item = selectedImage[position]; if (convertView == null) { convertView = activity.LayoutInflater.Inflate(Resource.Layout.SelectImageItem, parent, false); holder = new ViewHolder(); holder.ivEventPhoto = convertView.FindViewById <ImageView>(Resource.Id.ivSelectedItemEventPhoto); holder.ivEventVideo = convertView.FindViewById <ImageView>(Resource.Id.ivSelectItem_video); convertView.Tag = holder; } else { holder = convertView.Tag as ViewHolder; } Bitmap thumb = null; holder.ivEventPhoto.SetImageResource(0); if (type == 1) { holder.ivEventVideo.Visibility = ViewStates.Visible; thumb = ThumbnailUtils.CreateVideoThumbnail(item, Android.Provider.ThumbnailKind.MiniKind); if (thumb != null) { holder.ivEventPhoto.SetImageBitmap(thumb); } } else { Picasso.With(activity).Load(new Java.IO.File(item)).Placeholder(Resource.Drawable.default_event_back) .Transform(new RoundedTransformation()) .Resize(150, 150).Into(holder.ivEventPhoto); holder.ivEventVideo.Visibility = ViewStates.Gone; } // Dispose of the Java side bitmap. return(convertView); }
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); if (resultCode == Result.Ok) { if (requestCode == REQUEST_TAKE_GALLERY_VIDEO) { horizontal_recycler_view.Visibility = ViewStates.Visible; var uri = data.Data; string path = BasicUtils.GetActualPathFromFile(uri, this); System.Diagnostics.Debug.WriteLine("Image path == " + path); Bitmap thumb = ThumbnailUtils.CreateVideoThumbnail(path, ThumbnailKind.MiniKind); AddUpdateImageModel addUpdateImage = new AddUpdateImageModel(); base64ImageCode = BasicUtils.GetInstance().ConvertBitmapToBase64(thumb); addUpdateImage.bitmap = thumb; addUpdateImage.ImageUrl = base64ImageCode; addUpdateImage.CountImage = imagesList.Count + 1; imagesList.Add(addUpdateImage); addUpdateImageAdapter.NotifyDataSetChanged(); horizontal_recycler_view.SmoothScrollToPosition(imagesList.Count()); } if (requestCode == CameraCaptureImageRequestCode) { PreviewCapturedImage(); } else if (requestCode == CameraCaptureVideoRequestCode) { horizontal_recycler_view.Visibility = ViewStates.Visible; var uri = data.Data; string path = BasicUtils.GetActualPathFromFile(uri, this); System.Diagnostics.Debug.WriteLine("Image path == " + path); Bitmap thumb = ThumbnailUtils.CreateVideoThumbnail(path, ThumbnailKind.MiniKind); AddUpdateImageModel addUpdateImage = new AddUpdateImageModel(); base64ImageCode = BasicUtils.GetInstance().ConvertBitmapToBase64(thumb); addUpdateImage.bitmap = thumb; addUpdateImage.ImageUrl = base64ImageCode; addUpdateImage.CountImage = imagesList.Count + 1; imagesList.Add(addUpdateImage); addUpdateImageAdapter.NotifyDataSetChanged(); horizontal_recycler_view.SmoothScrollToPosition(imagesList.Count()); } else if (requestCode == PicFromGallery) { fileUri = data.Data; Bitmap bitmap = MediaStore.Images.Media.GetBitmap(ContentResolver, fileUri); GetImageFromGallery(); } else if (requestCode == PicCrop && data != null) { //get the returned data Android.Net.Uri extras = data.Data; //get the cropped bitmap if (extras != null) { var inp = context.ContentResolver.OpenInputStream(extras); BitmapFactory.Options options = new BitmapFactory.Options(); Bitmap cropped_bitmap = BitmapFactory.DecodeStream(inp, null, options); newBitmap = cropped_bitmap; getImageBitmapAsync(newBitmap); } else { var inp = context.ContentResolver.OpenInputStream(fileUri); BitmapFactory.Options options = new BitmapFactory.Options(); //Bitmap cropped_bitmap = MemoryStream stream = new MemoryStream(); BitmapFactory.DecodeStream(inp, null, options).Compress(Bitmap.CompressFormat.Jpeg, 50, stream); byte[] bitmapData = stream.ToArray(); Bitmap bitmap = BitmapFactory.DecodeByteArray(bitmapData, 0, bitmapData.Length); newBitmap = bitmap; getImageBitmapAsync(newBitmap); } } } }
MediaFile CreateMediaFileFromUri(Android.Net.Uri uri) { MediaFile mediaFile = null; var type = CrossCurrentActivity.Current.Activity.ContentResolver.GetType(uri); var path = GetRealPathFromURI(uri); if (path != null) { string fullPath = string.Empty; string thumbnailImagePath = string.Empty; string onbaseImagePath = string.Empty; var fileName = System.IO.Path.GetFileNameWithoutExtension(path); var ext = System.IO.Path.GetExtension(path) ?? string.Empty; MediaFileType mediaFileType = MediaFileType.Image; if (type.StartsWith(Enum.GetName(typeof(MediaFileType), MediaFileType.Image), StringComparison.CurrentCultureIgnoreCase)) { string watermark = Globals.IncluirWatermarkEnFotosGalería ? WatermarkHelper.ArmaWatermark() : null; var fullImage = ImageHelpers.RotateImage(path, 1, 90, watermark); var thumbImage = ImageHelpers.RotateImage(path, 0.25f, 90); var onbaseImage = ImageHelpers.RotateImage(path, 1, Globals.PorcentajeCompresion, watermark); fullPath = FileHelper.GetOutputPath(MediaFileType.Image, TemporalDirectoryName, $"{fileName}{ext}"); File.WriteAllBytes(fullPath, fullImage); thumbnailImagePath = FileHelper.GetOutputPath(MediaFileType.Image, TemporalDirectoryName, $"{fileName}-THUMBNAIL{ext}"); File.WriteAllBytes(thumbnailImagePath, thumbImage); onbaseImagePath = FileHelper.GetOutputPath(MediaFileType.Image, TemporalDirectoryName, $"{fileName}-ONBASE{ext}"); File.WriteAllBytes(onbaseImagePath, onbaseImage); } else if (type.StartsWith(Enum.GetName(typeof(MediaFileType), MediaFileType.Video), StringComparison.CurrentCultureIgnoreCase)) { fullPath = path; var bitmap = ThumbnailUtils.CreateVideoThumbnail(path, ThumbnailKind.MiniKind); thumbnailImagePath = FileHelper.GetOutputPath(MediaFileType.Image, TemporalDirectoryName, $"{fileName}-THUMBNAIL{ext}"); var stream = new FileStream(thumbnailImagePath, FileMode.Create); bitmap?.Compress(Bitmap.CompressFormat.Jpeg, 100, stream); stream.Close(); mediaFileType = MediaFileType.Video; } if (!string.IsNullOrEmpty(fullPath) && !string.IsNullOrEmpty(thumbnailImagePath) && !string.IsNullOrEmpty(onbaseImagePath)) { mediaFile = new MediaFile() { Id = System.Guid.NewGuid().ToString(), Path = fullPath, Type = mediaFileType, PreviewPath = thumbnailImagePath, OnBasePath = onbaseImagePath }; } } return(mediaFile); }
private void StartNotification() { Intent playintent = new Intent(AndroidApp.Context, typeof(Receiver)); playintent.PutExtra(ResourceName, "playButton"); Intent beforeintent = new Intent(AndroidApp.Context, typeof(Receiver)); beforeintent.PutExtra(ResourceName, "beforeButton"); Intent nextIntent = new Intent(AndroidApp.Context, typeof(Receiver)); nextIntent.PutExtra(ResourceName, "nextButton"); Intent closeIntent = new Intent(AndroidApp.Context, typeof(Receiver)); closeIntent.PutExtra(ResourceName, "closeButton"); Intent titleIntent = new Intent(AndroidApp.Context, typeof(MainActivity)); titleIntent.PutExtra(ResourceName, "contentTitle"); PendingIntent playPendingIntent = PendingIntent.GetBroadcast(AndroidApp.Context, 1, playintent, PendingIntentFlags.UpdateCurrent); PendingIntent beforePendingIntent = PendingIntent.GetBroadcast(AndroidApp.Context, 2, beforeintent, PendingIntentFlags.UpdateCurrent); PendingIntent nextPendingIntent = PendingIntent.GetBroadcast(AndroidApp.Context, 3, nextIntent, PendingIntentFlags.UpdateCurrent); PendingIntent closePendingIntent = PendingIntent.GetBroadcast(AndroidApp.Context, 4, closeIntent, PendingIntentFlags.UpdateCurrent); PendingIntent titlePendingIntent = PendingIntent.GetActivity(AndroidApp.Context, 5, titleIntent, PendingIntentFlags.UpdateCurrent); NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidApp.Context, channelId) .SetContentText(NowPlay.Title) .SetLargeIcon(ThumbnailUtils.CreateVideoThumbnail(NowPlay.path, ThumbnailKind.MiniKind)) .SetSmallIcon(Resource.Drawable.icon) .SetContentIntent(titlePendingIntent) .SetShowWhen(false) .SetAutoCancel(true) .AddAction(Resource.Drawable.prev_mini, "", beforePendingIntent); if (NowPlay.Audio.IsPlaying) { builder.AddAction(Resource.Drawable.pause_mini, "", playPendingIntent); } else { builder.AddAction(Resource.Drawable.play_mini, "", playPendingIntent); } builder.AddAction(Resource.Drawable.next_mini, "", nextPendingIntent) .AddAction(Resource.Drawable.close_mini, "", closePendingIntent) .SetStyle(new MediaStyle() .SetShowActionsInCompactView(0, 1, 2)); var notification = builder.Build(); notification.Flags = NotificationFlags.NoClear; var manager = (Android.App.NotificationManager)AndroidApp.Context.GetSystemService(AndroidApp.NotificationService); if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { var channelNameJava = new Java.Lang.String(channelName); var channel = new NotificationChannel(channelId, channelNameJava, NotificationImportance.Default) { Description = channelDescription }; channel.SetSound(null, null); channel.SetVibrationPattern(new long[] { 0 }); channel.EnableVibration(true); manager.CreateNotificationChannel(channel); } StartForeground(serviceNotifID, notification); }