use*_*474 7 camera android xamarin.forms
我从这里使用Xamarin.Forms Camera示例 - https://github.com/XForms/Xamarin-Forms-Labs-Samples/tree/master/XF.Labs.CameraSample我可以选择或拍照.
private async Task SelectPicture()
{
mediaPicker = DependencyService.Get<IMediaPicker>();
imageSource = null;
var mediaFile = await mediaPicker.SelectPhotoAsync(new CameraMediaStorageOptions
{
DefaultCamera = CameraDevice.Front,
MaxPixelDimension = 400
});
imageSource = ImageSource.FromStream(() => mediaFile.Source);
img.Source = imageSource;
}
private async Task TakePicture()
{
mediaPicker = DependencyService.Get<IMediaPicker>();
imageSource = null;
var mediaFile = await mediaPicker.TakePhotoAsync(new CameraMediaStorageOptions
{
DefaultCamera = CameraDevice.Front,
MaxPixelDimension = 400
});
imageSource = ImageSource.FromStream(() => mediaFile.Source);
img.Source = imageSource;
}
Run Code Online (Sandbox Code Playgroud)
图像的代码很简单
img = new Image
{
BackgroundColor = Color.White,
Aspect = Aspect.AspectFit
};
Run Code Online (Sandbox Code Playgroud)
有几个问题:
第一.您可以拍照或选择存储的照片然后将其显示在页面上.如果选择照片,则会正确显示照片,无论是纵向还是横向.拍摄照片时,它仅以横向模式显示,因此如果图像是以纵向拍摄的,则图像会显示在侧面.这不是灾难性的,但最好是向图像显示它是如何拍摄的.
第二个问题有点激烈,如果您在相机或图库中按下设备的后退按钮,则屏幕变为空白,然后您会收到一条消息,指出应用已停止响应.
到目前为止我只在Android上试过这个.有没有人对如何解决上述问题有任何想法?
编辑:我已设法修复后退按钮上的崩溃,但图像仍然显示在Android的侧面,但正确显示iOS
我大胆猜测这里有几个问题。Android 上的 Xamarin.Forms.Labs 依赖注入处理程序之一是 1) 不检查所需的轮换,2) 不检查外部存储或不处理 onActivityCancelled。
解决您的问题的简单方法是使用 Xamarin.Mobile Xamarin.Mobile我无法 100% 确认它能处理所有问题,但如果确实如此,这将是一个快速且简单的解决方案。
为您提供更多控制权的更困难的选择是推出您自己的特定于平台的实现。我不会深入探讨 DI 的工作原理,您知道或可以看到访问本机功能
下面是一个 Android 拍照示例,其中包含用于确定存储是否为外部以及是否需要旋转的代码。
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
//FinishActivity(requestCode);
try
{
if (resultCode == Result.Ok)
{
switch (requestCode)
{
case TAKE_PHOTO:
{
Java.IO.File photo = null;
if (isMounted)
{
photo = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.ToString(), SharedLibrary.strPhotoLocation);
}
else
{
photo = new Java.IO.File(CacheDir, SharedLibrary.strPhotoLocation);
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.InJustDecodeBounds = true;
options.InSampleSize = 4;
options.InPurgeable = true;
options.InInputShareable = true;
try
{
//Cleanup code... removed this in favor of using options.InJustDecodeBounds to get info about the bitmap
//instead of creating it twice in memory
//Bitmap imageBitmap = BitmapFactory.DecodeFile(photo.AbsolutePath, options);
//int w = imageBitmap.Width;
//int h = imageBitmap.Height;
BitmapFactory.DecodeFile(photo.AbsolutePath, options);
int w = options.OutWidth;
int h = options.OutHeight;
Matrix matrix = new Matrix();
matrix.SetRotate(getNeededRotation(photo.AbsolutePath));
options.InJustDecodeBounds = false;
//Bitmap imageBitmap = Bitmap.CreateBitmap(BitmapFactory.DecodeFile(photo.AbsolutePath, options), 0, 0, w, h, matrix, false);
Bitmap imageBitmap = Bitmap.CreateScaledBitmap(BitmapFactory.DecodeFile(photo.AbsolutePath, options), w, h, false);
//...
Run Code Online (Sandbox Code Playgroud)
安装
private System.Boolean isMounted
{
get
{
return (System.Boolean)Android.OS.Environment.ExternalStorageState.Equals(Android.OS.Environment.MediaMounted);
}
}
Run Code Online (Sandbox Code Playgroud)
获取所需旋转
private int getNeededRotation(string filepath)
{
ExifInterface exif = new ExifInterface(filepath);
int orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, -1);
int rotate = 0;
switch (orientation)
{
case 6:
{
rotate = 90;
break;
}
case 3:
{
rotate = 180;
break;
}
case 8:
{
rotate = 270;
break;
}
default:
{
rotate = 0;
break;
}
}
exif.Dispose();
return rotate;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7277 次 |
| 最近记录: |