是什么决定了相机的表面纹理宽度和高度?

时间:2016-12-10 04:26:20

标签: c# android xamarin xamarin.android android-camera

这是我实现自定义相机页面的片段。

出于某种原因,我不断获得比我预期的更低分辨率的textureView / SurfaceTexture高度和宽度。我想保持手机本机最大相机分辨率(或图片尺寸)正常使用。但是,按照我现在设置的方式,我的textureView的分辨率低于原生相机分辨率。我想知道为什么?我试图调试,似乎OnSurfaceTextureAvailable()似乎错误地设置宽度和高度。从哪里抓起来的?

public class CameraPage : PageRenderer, TextureView.ISurfaceTextureListener, Android.Views.View.IOnTouchListener 
{
    global::Android.Hardware.Camera camera;

    Activity activity;
    CameraFacing cameraType;
    TextureView textureView;
    SurfaceTexture surfaceTexture;

    public void OnSurfaceTextureAvailable (SurfaceTexture surface, int width, int height)
    {
        GetCameraInstance();

        if (camera != null) {
            textureView.LayoutParameters = new FrameLayout.LayoutParams(width, height);
            surfaceTexture = surface;

            camera.SetPreviewTexture(surface);

            PrepareAndStartCamera();
        }
    }

    private void GetCameraInstance()
    {
        try {
            camera = global::Android.Hardware.Camera.Open((int)CameraFacing.Back);
        }
        catch (Exception e) {
            //ignore any exception
        }
    }

    public bool OnSurfaceTextureDestroyed (SurfaceTexture surface)
    {
        StopCameraPreviewAndRelease();
        return true;
    }

     private void StopCameraPreview()
    {
        try {
            if (camera != null)
                camera.StopPreview();
        }
        catch { }
    }

    private void StopCameraPreviewAndRelease()
    {
        try {
            if (camera != null) {
                StopCameraPreview();
                camera.SetPreviewCallback(null);
                camera.Release();
                camera = null;
            }
        }
        catch { }
    }

    public void OnSurfaceTextureSizeChanged (SurfaceTexture surface, int width, int height)
    {
        PrepareAndStartCamera ();
    }

    public void OnSurfaceTextureUpdated (SurfaceTexture surface)
    {

    }

    private void PrepareAndStartCamera ()
    {
            var flashMode = GetFlashMode();
            SetCameraParameters(flashMode);

            StopCameraPreview();

            var display = activity.WindowManager.DefaultDisplay;
            if (display.Rotation == SurfaceOrientation.Rotation0) {
                camera.SetDisplayOrientation (90);
            }

            if (display.Rotation == SurfaceOrientation.Rotation270) {
                camera.SetDisplayOrientation (180);
            }

            if (flashOn)
                toggleFlashButton.SetBackgroundResource(Resource.Drawable.flash_on);
            else
                toggleFlashButton.SetBackgroundResource(Resource.Drawable.flash_off);

            camera.StartPreview ();
    }
}

这就是我设置textureView的方法:

textureView = view.FindViewById<TextureView> (Resource.Id.textureView);
textureView.SurfaceTextureListener = this;

我的cameraLayout中的textureView:

<TextureView
        android:id="@+id/textureView"
        android:layout_marginTop="-110dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="#99b4d1ff"
        android:layout_marginLeft="0dp" />

也就是说,拍摄并保存照片时,我期待2576x1932分辨率。但是当拍摄并保存照片时,我得到了1451x720。似乎这被确定为textureView大小(但我想要原生相机分辨率大小)。

编辑:以下是拍摄照片的方式:

private async void TakePhotoButtonTapped (object sender, EventArgs e)
{
    try{
        try
        {
            StopCameraPreview();
        }
        catch (Exception ex) {
            camera.Reconnect();
            PrepareAndStartCamera();
            StopCameraPreview();
        }

        var image = textureView.Bitmap;

        var imageQuality = AppState.ApplicationInfo.AndroidImageCompressionFactor;
        using (var imageStream = new MemoryStream ()) {
            await image.CompressAsync(Bitmap.CompressFormat.Jpeg, imageQuality, imageStream);
            image.Recycle();
            imageBytes = imageStream.ToArray ();
        }
        count +=1;

        textView.Text = Convert.ToString(count);
        _images.Add(imageBytes);
        camera.StartPreview ();
    }
    catch(Exception ex)
    {
    }
}

0 个答案:

没有答案