【问题标题】:How to add more work for 1 hit of "take image" button for camera app?如何为相机应用程序的 1 次“拍摄图像”按钮添加更多工作?
【发布时间】:2017-10-17 16:06:13
【问题描述】:

我正在尝试使用 Xamarin 在 Android 上构建相机应用。我想要做的是当用户点击“拍照”按钮时,相机会自动将后置摄像头切换到前置摄像头并捕捉图像,这意味着“拍照 strong>" 按钮将做两件事:同时切换相机和捕获图像。

我是 Xamarin 的新手,正在开发 Android 应用程序。我搜索了很多教程来构建相机应用程序,但它们似乎很简单,当用户点击“take image”按钮时,我没有看到任何覆盖功能。这是我在主要活动中的代码(只是为了构建简单的相机应用程序):

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);
    }

任何想法都会有所帮助,非常感谢。

【问题讨论】:

    标签: android xamarin.android android-camera


    【解决方案1】:

    意图意图 = 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更改相机的前置/后置摄像头。

    请参考这个帖子:Android: Switch camera when button clicked 。 Xamarin Android 的代码可能与 Java 中的代码相同。

    这意味着“拍照”按钮将做两件事:同时切换相机和捕捉图像。

    顺便说一句,我认为这不是一个好主意,让您的应用程序连线。你为什么要这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      • 2013-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多