如何在没有预览的情况下拍摄照片。 Android系统。 Xamarin

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在没有预览的情况下拍摄照片。 Android系统。 Xamarin相关的知识,希望对你有一定的参考价值。

如何在没有预览的情况下单击按钮拍摄照片?我是Xamarin的新手,所以如果我能获得尽可能多的细节,它会很棒。

答案

在.axml文件中使用它:

<SurfaceView
        android:id="@+id/sfv"
        android:layout_height="1dp"
        android:layout_width="1dp" />

或者用它来设置c#代码中的SurfaceView

IWindowManager windowManager = GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
WindowManagerFlags flags = WindowManagerFlags.NotFocusable | WindowManagerFlags.NotTouchModal | WindowManagerFlags.NotTouchable;
var layoutParams = new WindowManagerLayoutParams(1, 1,
            WindowManagerTypes.SystemAlert,
            flags,
            Format.Rgb888);
SurfaceView sfv = new SurfaceView(this);
//mll is LinearLayout
mll.AddView(sfv,layoutParams);

更新:

活动:

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Views;
using System;
using Android.Content;
using Android.Runtime;
using Camera = Android.Hardware.Camera;
using Android.Graphics;
using static Android.Hardware.Camera;
using Java.IO;

namespace Cam
{
    [Activity(Label = "Cam", MainLauncher = true)]
    public class MainActivity : Activity, ISurfaceHolderCallback
    {
       private LinearLayout mll;
       private Button button;
       private Camera camera;
        private ISurfaceHolder mHolder;
       private SurfaceView sfv;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            initView();
            initListener();
        }

        protected override void OnResume()
        {
            base.OnResume();
            if (null == camera) {
                camera = getCustomCamera();
                if (mHolder != null) {
                    previceCamera(camera,mHolder);
                }

            }

        }

        protected override void OnStop()
        {
            base.OnStop();
            if (null != camera) {
                camera.SetPreviewCallback(null);
                camera.StopPreview();
                camera.Release();
                camera = null;
            }

        }
        private void previceCamera(Camera camera, ISurfaceHolder mHolder)
        {
            camera.SetPreviewDisplay(mHolder);
            camera.SetDisplayOrientation(90);
            camera.StartPreview();
        }

        private Camera getCustomCamera()
        {
            if (null == camera) {
                camera = Camera.Open();
            }
            return camera;

        }

        private void initListener()
        {
            button.Click += picture;
        }

        private void picture(object sender, EventArgs e)
        {
            var parameters = camera.GetParameters();
            parameters.PictureFormat = ImageFormatType.Jpeg;
            parameters.SetPreviewSize(480,720);
            parameters.FocusMode = Camera.Parameters.FocusModeAuto;
            camera.AutoFocus(new MyFocusCallback(camera));

        }


        private void initView()
        {
            mll = FindViewById<LinearLayout>(Resource.Id.ll);
            button = FindViewById<Button>(Resource.Id.bt);
            mFloatView();
            mHolder = sfv.Holder;
            mHolder.AddCallback(this);
        }

        public void mFloatView() {

            IWindowManager windowManager = GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
            WindowManagerFlags flags = WindowManagerFlags.NotFocusable | WindowManagerFlags.NotTouchModal | WindowManagerFlags.NotTouchable;
            var layoutParams = new WindowManagerLayoutParams(1, 1,
            WindowManagerTypes.SystemAlert,
            flags,
            Format.Rgb888);
            sfv = new SurfaceView(this);
            //mll is LinearLayout
            mll.AddView(sfv,layoutParams);

        }



        public void SurfaceCreated(ISurfaceHolder holder)
        {
            previceCamera(camera,holder);
        }

        public void SurfaceDestroyed(ISurfaceHolder holder)
        {
            if (null != camera) {
                camera.SetPreviewCallback(null);
                camera.StopPreview();
                camera.Release();
                camera = null;
            }


        }

        public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height)
        {
            camera.StopPreview();
            previceCamera(camera, holder);
        }
    }

    public class MyFocusCallback :Java.Lang.Object, IAutoFocusCallback
    {
        Camera mCamera;
        public MyFocusCallback(Camera camera){
            mCamera = camera;
        }
        public void Dispose()
        {
        }

        public void OnAutoFocus(bool success, Camera camera)
        {
            if (success) {
                mCamera.TakePicture(null,null,new MyPictureCallback());
            }
        }
    }

    public class MyPictureCallback : Java.Lang.Object,IPictureCallback
    {

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void OnPictureTaken(byte[] data, Camera camera)
        {
            dealWithCameraData(data);
        }

        private void dealWithCameraData(byte[] data)
        {
            // Here to save your photo
            FileOutputStream fos = null;
            string tempStr = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            string fileName = tempStr + Java.Lang.JavaSystem.CurrentTimeMillis() + ".jpg";
            File tempFile = new File(fileName);
            fos = new FileOutputStream(fileName);
            fos.Write(data);
            fos.Close();

        }
    }
}

.axml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ll">
    <Button
        android:id="@+id/bt"
        android:text="take a photo"
        android:layout_height="wrap_content"
        android:layout_width="match_parent" />
    <SurfaceView
        android:id="@+id/sfv"
        android:layout_height="1dp"
        android:layout_width="1dp" />
</LinearLayout>

在项目中选择一种方式(.axml文件或c#代码)if you run it on Android M and above, please use runtime permission, or you will get permission denied

以上是关于如何在没有预览的情况下拍摄照片。 Android系统。 Xamarin的主要内容,如果未能解决你的问题,请参考以下文章

Android:在没有用户界面的情况下拍照

Android:拍摄的照片比预览大

如何在不保存/显示图像的情况下拍照

如何使用 Cordova 相机插件拍摄多张照片

Android相机第二次拍摄失败

Android相机照片预览正在逆转