如何添加更多的工作为1相机应用程序的“拍照”按钮点击?
问题描述:
我正试图在Xamarin上使用Android构建相机应用程序。我想要做的是当用户点击“拍摄图像”按钮时,相机会自动将后置摄像头切换到前置摄像头并拍摄图像,这意味着“拍摄图像”按钮将做2件事:切换相机和捕捉图像在同一时间。如何添加更多的工作为1相机应用程序的“拍照”按钮点击?
我是新来的Xamarin和开发Android应用程序。我已经搜索了很多教程来构建相机应用程序,但它们看起来很简单,当用户点击“拍摄图像”按钮时,我看不到任何覆盖功能。在这里我的主要活动代码(只是为了建立简单的摄像头应用程序):
ImageView imageView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
var btnCamera = FindViewById<Button>(Resource.Id.btnCamera);
imageView = FindViewById<ImageView>(Resource.Id.imageView);
btnCamera.Click += BtnCamera_Click;
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
Bitmap bitmap = (Bitmap)data.Extras.Get("data");
imageView.SetImageBitmap(bitmap);
}
// When user tap on the button, open camra app
private void BtnCamera_Click(object sender, System.EventArgs e)
{
Intent intent = new Intent(MediaStore.ActionImageCapture);
StartActivityForResult(intent, 0);
}
任何想法会有所帮助,非常感谢。
答
Intent intent = new Intent(MediaStore.ActionImageCapture);
此方法使用系统的相机应用程序,您不能使用此方法更改此处的前/后相机。
您可以阅读本教程:Display a stream from the camera,它展示了如何使用Android.Hardware.Camera构建您自己的相机应用程序。
要将Button
添加到视图,以便用户可以拍照,你可能会喜欢这个创建例如你的相机预览视图:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextureView
android:id="@+id/textureView"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<Button
android:id="@+id/takephotoBtn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="take photo" />
</RelativeLayout>
您可以通过Android.Hardware.Camera.Open Method改变相机的前/后摄像头。
请参阅SO上的此主题:Android: Switch camera when button clicked 。 Xamarin Android的代码可能与Java中的代码相同。
这意味着“拍摄图像”按钮将做2件事:同时切换摄像头和拍摄图像。
顺便说一句,我不认为这是一个好主意,让您的应用程序有线。你为什么想这样做。