Android Camera2 Preview is rotated 90deg while in Landscape

纵饮孤独 提交于 2019-12-05 19:45:19
private void transformImage (int width, int height)
{
    if(mPreviewSize == null || mTextureView == null)
    {
        return;
    }
    Matrix matrix = new Matrix();
    int rotation = getWindowManager().getDefaultDisplay().getRotation();
    RectF textureRectF = new RectF(0,0,width,height);
    RectF previewRectF = new RectF(0,0,mPreviewSize.getHeight(),mPreviewSize.getWidth());
    float centerX = textureRectF.centerX();
    float centery = textureRectF.centerY();

    if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270)
    {}
    else if(rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270)
    {
        previewRectF.offset(centerX - previewRectF.centerX(),centery-previewRectF.centerY());
        matrix.setRectToRect(textureRectF,previewRectF,Matrix.ScaleToFit.FILL);
        float scale = Math.max((float)width / mPreviewSize.getWidth(),(float)height/ mPreviewSize.getHeight());

        matrix.postScale(scale,scale,centerX,centery);
        matrix.postRotate(90*(rotation-2),centerX,centery);
        mTextureView.setTransform(matrix );

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!