Android camera2预览无法在横向模式下正常工作

Android camera2预览无法在横向模式下正常工作,第1张

概述我只想在textureView中显示相机预览. 在纵向模式下它看起来很好但在横向模式下它顺时针旋转90度. 我知道这样的事情可以解决这个问题: private static final SparseIntArray ORIENTATIONS = new SparseIntArray();static { ORIENTATIONS.append(Surface.ROTATION_0, 9 我只想在textureVIEw中显示相机预览.
在纵向模式下它看起来很好但在横向模式下它顺时针旋转90度.
我知道这样的事情可以解决这个问题:

private static final SparseIntArray ORIENTATIONS = new SparseIntArray();static {    ORIENTATIONS.append(Surface.ROTATION_0,90);    ORIENTATIONS.append(Surface.ROTATION_90,0);    ORIENTATIONS.append(Surface.ROTATION_180,270);    ORIENTATIONS.append(Surface.ROTATION_270,180);}

我不知道在哪里打电话.你有什么建议吗?

这是我的代码:

private TextureVIEw textureVIEw;private static final String TAG = "AndroIDCameraAPI";private String cameraID;protected CameraDevice cameraDevice;protected CameraCaptureSession cameraCaptureSessions;protected CaptureRequest captureRequest;private static final int REQUEST_CAMERA_PERMISSION = 200;private Size imageDimension;protected CaptureRequest.Builder captureRequestBuilder;private Handler mBackgroundHandler;private HandlerThread mBackgroundThread;private ImageReader imageReader;private static final SparseIntArray ORIENTATIONS = new SparseIntArray();static {    ORIENTATIONS.append(Surface.ROTATION_0,180);}@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_photo);    textureVIEw = (TextureVIEw) findVIEwByID(R.ID.texturePhoto);    assert textureVIEw != null;    textureVIEw.setSurfaceTextureListener(textureListener);}TextureVIEw.SurfaceTextureListener textureListener = new TextureVIEw.SurfaceTextureListener() {    @OverrIDe    public voID onSurfaceTextureAvailable(SurfaceTexture surface,int wIDth,int height) {        //open your camera here        openCamera();    }    @OverrIDe    public voID onSurfaceTextureSizeChanged(SurfaceTexture surface,int height) {        // transform you image captured size according to the surface wIDth and height    }    @OverrIDe    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {        return false;    }    @OverrIDe    public voID onSurfaceTextureUpdated(SurfaceTexture surface) {    }};private voID openCamera() {    CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);    Log.e(TAG,"is camera open");    try {        cameraID = manager.getCameraIDList()[0];        Cameracharacteristics characteristics = manager.getCameracharacteristics(cameraID);        StreamConfigurationMap map = characteristics.get(Cameracharacteristics.SCALER_STREAM_CONfigURATION_MAP);        assert map != null;        imageDimension = map.getoutputSizes(SurfaceTexture.class)[0];        // Add permission for camera and let user grant the permission        if (ActivityCompat.checkSelfPermission(this,Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {            ActivityCompat.requestPermissions(PhotoActivty.this,new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_CAMERA_PERMISSION);            return;        }        manager.openCamera(cameraID,stateCallback,null);    } catch (CameraAccessException e) {        e.printstacktrace();    }    Log.e(TAG,"openCamera X");}private final CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {    @OverrIDe    public voID onopened(CameraDevice camera) {        //This is called when the camera is open        Log.e(TAG,"onopened");        cameraDevice = camera;        createCameraPrevIEw();    }    @OverrIDe    public voID ondisconnected(CameraDevice camera) {        cameraDevice.close();    }    @OverrIDe    public voID onError(CameraDevice camera,int error) {        cameraDevice.close();        cameraDevice = null;    }};protected voID createCameraPrevIEw() {    try {        SurfaceTexture texture = textureVIEw.getSurfaceTexture();        assert texture != null;        texture.setDefaultBufferSize(imageDimension.getWIDth(),imageDimension.getHeight());        Surface surface = new Surface(texture);        captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);        captureRequestBuilder.addTarget(surface);        cameraDevice.createCaptureSession(Arrays.asList(surface),new CameraCaptureSession.StateCallback(){            @OverrIDe            public voID onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {                //The camera is already closed                if (null == cameraDevice) {                    return;                }                // When the session is ready,we start displaying the prevIEw.                cameraCaptureSessions = cameraCaptureSession;                updatePrevIEw();            }            @OverrIDe            public voID onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {                Toast.makeText(PhotoActivty.this,"Configuration change",Toast.LENGTH_SHORT).show();            }        },null);    } catch (CameraAccessException e) {        e.printstacktrace();    }}protected voID updatePrevIEw() {    if(null == cameraDevice) {        Log.e(TAG,"updatePrevIEw error,return");    }    captureRequestBuilder.set(CaptureRequest.CONTRol_MODE,CameraMetadata.CONTRol_MODE_auto);    try {        cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(),null,mBackgroundHandler);    } catch (CameraAccessException e) {        e.printstacktrace();    }}private voID closeCamera() {    if (null != cameraDevice) {        cameraDevice.close();        cameraDevice = null;    }    if (null != imageReader) {        imageReader.close();        imageReader = null;    }}
解决方法 您需要使用正确的矩阵调用TextureVIEw.settransform().

请参阅Google的Camera2Basic示例和函数Camera2BasicFragment.configureTransform().

总结

以上是内存溢出为你收集整理的Android camera2预览无法在横向模式下正常工作全部内容,希望文章能够帮你解决Android camera2预览无法在横向模式下正常工作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1123904.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存