将相机 SurfaceView 旋转为纵向
我查找了一些关于使用表面视图更改相机方向的帖子,但我从以下示例中获取了代码:
http://developer.android.com /resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html
提供尺寸的函数如下所示...
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
我的问题是,当我更改设备上,预览图片保持横向。我尝试设置相机的方向,但这导致了非常奇怪的结果。有谁知道我需要更改什么才能正确旋转?
I have looked up a few posts on changing the orientation of the camera with a surface view, but I have taken my code from the examples at:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html
The function that provides the dimensions looks like this...
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
My problem is that when I change the orientation of the device, the preview picture stays landscape. I tried setting the orientation of the camera but this resulted in very strange results. Does anyone know what I need to change to have this rotate properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
正确调整相机预览方向的代码有点复杂,因为它必须考虑
Camera.setDisplayOrientation 有有关如何正确处理此问题的示例代码。我在这里重现它:
在绘制 UI(onSurfaceChanged 将用作指示器)或设备 UI 旋转(onConfigurationChanged 将用作指示器)后调用此方法。
The code to correctly adjust the camera preview orientation is a bit complex, since it has to take into account
The documentation for Camera.setDisplayOrientation has sample code on how to deal with this correctly. I'm reproducing it here:
Call this after your UI has been drawn (onSurfaceChanged would work as an indicator) or the device UI rotates (onConfigurationChanged would work as an indicator).
我能够通过将以下代码放入 onSurfaceChanged() 中来解决旋转问题:
但这产生了另一个问题,或者更好地说,没有解决问题 - 虽然方向正确,但预览图像仍然只占用相同数量的景观景观所做的空间。我最终只是放弃并强制横向:
在 onCreate() 中并设计了横向布局。祝你好运,我希望有人能解决这个次要问题!
I was able to solve the rotation problem by putting the following code in onSurfaceChanged():
BUT this created another problem, or better put, didn't solve a problem-while oriented correctly, the preview image still only took up the same amount of space that the landscape view did. I ended up just giving up and forcing landscape orientation with:
in onCreate() and designed my layout for landscape. Best of luck, and I hope someone has an answer for this secondary problem!
试试这个,但我在 Samsung Galaxy Tab 上试过
Try this out, but I tried in Samsung Galaxy Tab
我通过添加以下内容解决了这个问题:
到 onCreate 事件。
I solved this issue by adding:
To the onCreate event.
请尝试这个..
Please try this..
好吧,我这样做!
我已经在 surfaceCreated() 方法中以这种方式解决了这个问题,
在下面的 3 中添加此方法
:确保您已添加相机权限以支持活动中的牛轧糖,在此活动调用不要在此活动中调用相机权限,因为 surfacecreated 调用为创建的活动,并且将等待您的许可才能打开相机
Well i Do this!
I have solved this issue in in this way in surfaceCreated() method
add this method below
3: Make sure you have added camera permiison to support for nougat in a activity before this activity calling dont call permission for camera in this activity because surfacecreated call as the activity created, and your permission will be pending to open camera
仅添加此
Only add this