簡體   English   中英

Camera2 預覽和捕獲的圖像看起來不同

[英]Camera2 preview and captured image looks different

默認情況下,我得到一個旋轉 -90 度的捕獲圖像,我需要將它旋轉回來,但它的高度與屏幕的高度不同,所以我的圖像沒有填滿屏幕。

在照片后的橫向模式下,它只顯示捕獲圖像的右上角..

我嘗試了所有可用的示例,但找不到解決方案。

private void SetUpCameraOutputs(int width, int height)
{
    _manager = (CameraManager)_context.GetSystemService(Context.CameraService);

    string[] cameraIds = _manager.GetCameraIdList();

    _cameraId = cameraIds[0];

    CameraCharacteristics chararc = _manager.GetCameraCharacteristics(cameraIds[i])
    var characteristics = _manager.GetCameraCharacteristics(_cameraId);
    var map = (StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap);

    if (_supportedJpegSizes == null && characteristics != null){
        _supportedJpegSizes = ((StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap)).GetOutputSizes((int)ImageFormatType.Jpeg);
    }

    if (_supportedJpegSizes != null && _supportedJpegSizes.Length > 0){
        _idealPhotoSize = GetOptimalSize(_supportedJpegSizes, 1050, 1400); 
    }

    _imageReader = ImageReader.NewInstance(_idealPhotoSize.Width, _idealPhotoSize.Height, ImageFormatType.Jpeg, 1);

    var readerListener = new ImageAvailableListener();

    readerListener.Photo += (sender, buffer) =>
    {
        Photo?.Invoke(this, buffer);
    };

    _flashSupported = HasFLash(characteristics);

    _imageReader.SetOnImageAvailableListener(readerListener, _backgroundHandler);
        
    _previewSize = GetOptimalSize(map.GetOutputSizes(Class.FromType(typeof(SurfaceTexture))), _idealPhotoSize.Height, _idealPhotoSize.Width);
}

拍照方法:

public void TakePhoto()
{
    if (_context == null || CameraDevice == null) return;

    if (_captureBuilder == null)
        _captureBuilder = CameraDevice.CreateCaptureRequest(CameraTemplate.StillCapture);

    _captureBuilder.AddTarget(_imageReader.Surface);

    _captureBuilder.Set(CaptureRequest.ControlAfMode, (int)ControlAFMode.ContinuousPicture);

    var windowManager = _context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
    var rotation = windowManager.DefaultDisplay.Rotation;
    _captureBuilder.Set(CaptureRequest.JpegOrientation, new Integer(Orientations.Get((int)rotation)));

    _previewSession.StopRepeating();
    _previewSession.Capture(_captureBuilder.Build(),
        new CameraCaptureStillPictureSessionCallback
        {
            OnCaptureCompletedAction = session =>
            {
                UnlockFocus();
            }
        }, null);
}

還有我的 OnPhoto 方法:

private void OnPhoto(object sender, byte[] imgSource)
{
    Android.Graphics.Bitmap bitmap = BitmapFactory.DecodeByteArray(imgSource, 0, imgSource.Length);
    var windowManager = _context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
    var rotation = windowManager.DefaultDisplay.Rotation;
    if (rotation == SurfaceOrientation.Rotation0 || rotation == SurfaceOrientation.Rotation180)
    {
        bitmap = resizeAndRotate(bitmap, bitmap.Width, bitmap.Height); //rotate bitmap by 90
    }

    var SkBitmap = bitmap.ToSKBitmap();

    Application.Current.Properties["bitmap"] = SkBitmap;

    Device.BeginInvokeOnMainThread(() =>
    {
        _currentElement?.PictureTaken();
    });
}

在為縱向模式創建位圖時,您必須更改位圖的寬度和高度。 所以寬度變成高度,高度變成縱向圖像的寬度。


int newWidth = oldHeight;
int newHeight = oldWidth;

Bitmap newBitmap  =Bitmap.createScaledBitmap(oldbitmap,newWidth,newHeight,true);

現在你已經完成了 newBitmap 的真實面貌,沒有拉伸。

根據您的代碼,只需使用以下命令恢復位圖寬度和高度:

bitmap = resizeAndRotate(bitmap, bitmap.height, bitmap.width);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM