android camera rotate,Android Camera2 preview occasionally rotated by 90

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

I'm working on some app using Android's Camera2 API. So far I've been able to get a preview displayed within a TextureView. The app is by default in landscape mode. When using the emulator the preview will appear upside-down. On my physical Nexus 5 the preview is usually displayed correctly (landscape, not upside-down), but occasionally it is rotated by 90 degrees, yet stretched to the dimensions of the screen.

I thought that should be easy and thought the following code would return the necessary information on the current orientation:

// display rotation

getActivity().getWindowManager().getDefaultDisplay().getRotation();

// sensor orientation

mManager.getCameraCharacteristics(mCameraId).get(CameraCharacteristics.SENSOR_ORIENTATION);

... I was pretty surprised when I saw that above code always returned 1 for the display rotation and 90 for the sensor orientation, regardless of the preview being rotated by 90 degree or not. (Within the emulator sensor orientation is always 270 which kinda makes sense if I assume 90 to be the correct orientation).

I also checked the width and height within onMeasure within AutoMeasureTextureView (adopted from Android's Camera2 example) that I'm using to create my TextureView. But no luck either - width and height reported from within onMeasure are always the same regardless of the preview rotation.

So I'm clueless on how to tackle this issue. Does anyone have an idea what could be the reason for the occasional hickups in my preview orientation?

[Edit]

A detail I just found out: Whenever the preview appears rotated onSurfaceTextureSizeChanged in the TextureView.SurfaceTextureListener seems not to get called. In the documentation for onSurfaceTextureSizeChanged it is said that this method is called whenever the SurfaceTexture's buffers size is changed. I have a method createCameraPreviewSession (copied from Android's Camera2 example) in which I set the default buffer size of my texture like

texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());

From my logging output I can tell that onSurfaceTextureSizeChanged is called exactly after that - however, not always... (or setting the default buffer size sometimes silently fails?).

回答1:

I think I can answer my own question: I was creating my Camera2 Fragment after the Android's Camera2 example. However, I didn't really consider the method configureTransform to be important as, opposite to the example code, my application is forced to landscape mode anyway. It turned out that this assumption was wrong. Since having configureTransform reintegrated in my code I haven't experienced any more hiccups.

Update: The original example within the Android documentation pages doesn't seem to exist anymore. I've updated the link which is now pointing to the code on Github.

回答2:

I had also faced the similar issue on Nexus device.Below code is working for me.

Call this function before opening the camera.And also on Resume().

private void transformImage(int width, int height)

{

if (textureView == null) {

return;

} else try {

{

Matrix matrix = new Matrix();

int rotation = getWindowManager().getDefaultDisplay().getRotation();

RectF textureRectF = new RectF(0, 0, width, height);

RectF previewRectF = new RectF(0, 0, textureView.getHeight(), textureView.getWidth());

float centerX = textureRectF.centerX();

float centerY = textureRectF.centerY();

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 / width, (float) height / width);

matrix.postScale(scale, scale, centerX, centerY);

matrix.postRotate(90 * (rotation - 2), centerX, centerY);

}

textureView.setTransform(matrix);

}

} catch (Exception e) {

e.printStackTrace();

}

}

回答3:

I followed the whole textureView.setTransform(matrix) method listed above, and it worked. However, I was also able to manually set the rotation using the much simpler textureView.setRotation(270) without the need to create a Matrix.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值