Xamarin Forms Android 视频压缩
我的应用程序的一部分(Xamarin Forms - 跨平台)需要减小视频文件的大小。 iOS 自己会做,但 Android 不会。对于照片,我是这样做的:
public byte[] CompressImage(byte[] imageData, int imageQuality, int imageWidth, int imageHeight)
{
int smallToBigWidth = imageWidth / 2;
int smallToBigHeight = imageHeight / 2;
Bitmap originalImage = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length);
float proportions = (float)imageHeight / (float)originalImage.Height;
float newWidth = (float)originalImage.Width * proportions;
originalImage = Bitmap.CreateScaledBitmap(originalImage, (int)newWidth, imageHeight, false);
using (MemoryStream ms = new MemoryStream())
{
originalImage.Compress(Bitmap.CompressFormat.Jpeg, imageQuality, ms);
return ms.ToArray();
}
}
但是如何处理视频,以便当您从图库中选择文件时,您会获得简化版本,而不会将新文件保存在手机上?我使用 MediaPicker 下载文件
var file = await MediaPicker.PickVideoAsync ();
并获取 FileResult 类型的文件,这就是我想要在压缩后实现的类型。有人有什么想法吗?我肯定到处都找过了,但什么也没有。
Part of my application (Xamarin Forms - crossplatform) needs to reduce the size of video files. iOS does it itself, but Android doesn't. For photos I do it like this:
public byte[] CompressImage(byte[] imageData, int imageQuality, int imageWidth, int imageHeight)
{
int smallToBigWidth = imageWidth / 2;
int smallToBigHeight = imageHeight / 2;
Bitmap originalImage = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length);
float proportions = (float)imageHeight / (float)originalImage.Height;
float newWidth = (float)originalImage.Width * proportions;
originalImage = Bitmap.CreateScaledBitmap(originalImage, (int)newWidth, imageHeight, false);
using (MemoryStream ms = new MemoryStream())
{
originalImage.Compress(Bitmap.CompressFormat.Jpeg, imageQuality, ms);
return ms.ToArray();
}
}
But how do you do it with a video so that when you select a file from the gallery, you get a reduced version without saving the new file on your phone? I download the file using MediaPicker
var file = await MediaPicker.PickVideoAsync ();
and gets a file of the FileResult type, and this is the type I would like to achieve after compression. Anyone have any ideas? I must have looked everywhere and nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 Android,您可以使用
Transcoder
。 https://github.com/natario1/Transcoder您还可以参考示例:https://github.com/leye0/XamarinAndroidFFmpeg
For Android, you could use
Transcoder
. https://github.com/natario1/TranscoderYou could also refer to sample: https://github.com/leye0/XamarinAndroidFFmpeg