我们正在使用Xamarin表单制作视频录制应用。
我的问题是如何以Xamarin格式从视频生成缩略图?
答案 0 :(得分:0)
在iOS上,您可以执行以下操作:
public ImageSource GenerateThumbImage(string url, long usecond)
{
AVAssetImageGenerator imageGenerator = new AVAssetImageGenerator(AVAsset.FromUrl((new Foundation.NSUrl(url))));
imageGenerator.AppliesPreferredTrackTransform = true;
CMTime actualTime;
NSError error;
CGImage cgImage = imageGenerator.CopyCGImageAtTime(new CMTime(usecond, 1000000), out actualTime, out error);
return ImageSource.FromStream(() => new UIImage(cgImage).AsPNG().AsStream());mageSource.FromStream(() => new UIImage(cgImage).AsPNG().AsStream());
}
在Android上:
public ImageSource GenerateThumbImage(string url, long usecond)
{
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.SetDataSource(url, new Dictionary<string, string>());
Bitmap bitmap = retriever.GetFrameAtTime(usecond);
if (bitmap != null)
{
MemoryStream stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
byte[] bitmapData = stream.ToArray();
return ImageSource.FromStream(() => new MemoryStream(bitmapData));
}
return null;
}