如何为相机应用程序的 "take image" 按钮点击 1 次添加更多工作?
How to add more work for 1 hit of "take image" button for camera app?
我正在尝试使用 Xamarin 在 Android 上构建相机应用。我想做的是当用户点击“拍照”按钮时,相机会自动将后置摄像头切换到前置摄像头并拍摄图像,这意味着“take image”按钮会做两件事:同时切换相机和拍摄图像。
我是 Xamarin 的新手,正在开发 Android 应用程序。我搜索了许多构建相机应用程序的教程,但它们似乎很简单,当用户点击“拍摄图像”按钮时,我没有看到任何覆盖功能。这是我在 Main activity 中的代码(只是为了构建简单的相机应用程序):
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, it shows how to use 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:单击按钮时切换相机
。 Xamarin Android 的代码可能与 Java 中的代码相同。
which means the "take image" button will do 2 things: switch camera and capture image at the same time.
顺便说一句,我不认为这是一个好主意,使您的应用程序有线。你为什么要这样做。
我正在尝试使用 Xamarin 在 Android 上构建相机应用。我想做的是当用户点击“拍照”按钮时,相机会自动将后置摄像头切换到前置摄像头并拍摄图像,这意味着“take image”按钮会做两件事:同时切换相机和拍摄图像。
我是 Xamarin 的新手,正在开发 Android 应用程序。我搜索了许多构建相机应用程序的教程,但它们似乎很简单,当用户点击“拍摄图像”按钮时,我没有看到任何覆盖功能。这是我在 Main activity 中的代码(只是为了构建简单的相机应用程序):
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, it shows how to use 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:单击按钮时切换相机 。 Xamarin Android 的代码可能与 Java 中的代码相同。
which means the "take image" button will do 2 things: switch camera and capture image at the same time.
顺便说一句,我不认为这是一个好主意,使您的应用程序有线。你为什么要这样做。