java 从视觉 API 预览条码扫描仪的尺寸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32999813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Preview size for barcode scanner from vision api
提问by MMagician
I'm using the barcode-reader example from Google's Android Vision API. The preview size doesn't seem to fill up the whole space available (I'm using a Nexus 4 and there is a white unused space to the right of preview, about 1/3 of the width).
我正在使用 Google 的 Android Vision API 中的条形码阅读器示例。预览大小似乎没有填满整个可用空间(我使用的是 Nexus 4,预览右侧有一个白色的未使用空间,大约是宽度的 1/3)。
I would like to be able to run this example on various devices and always have it fill up the whole space available.
我希望能够在各种设备上运行此示例,并始终让它填满整个可用空间。
So the bit I've been playing with is:
所以我一直在玩的一点是:
CameraSource.Builder builder = new CameraSource.Builder(getApplicationContext(), barcodeDetector).setFacing(CameraSource.CAMERA_FACING_BACK).setRequestedPreviewSize(?, ?).setRequestedFps(15.0f);
Any ideas?
有任何想法吗?
Thanks!
谢谢!
采纳答案by pm0733464
You might take a look at this thread: https://github.com/googlesamples/android-vision/issues/23
你可以看看这个线程:https: //github.com/googlesamples/android-vision/issues/23
回答by Akash Dubey
just remove or comment below code from CameraSourcePreview class
只需从 CameraSourcePreview 类中删除或评论下面的代码
if (childHeight > layoutHeight) {
childHeight = layoutHeight;
childWidth = (int)(((float) layoutHeight / (float) height) * width);
}
and use layoutHeight instead of childHeight of "CameraSourcePreview" class in this loop - for (int i = 0; i < getChildCount(); ++i){...}
并在此循环中使用 layoutHeight 而不是“CameraSourcePreview”类的 childHeight - for (int i = 0; i < getChildCount(); ++i){...}
if (mCameraSource != null)
{
Size size = mCameraSource.getPreviewSize();
if (size != null)
{
width = size.getWidth();
height = size.getHeight();
}
}
// Swap width and height sizes when in portrait, since it will be rotated 90 degrees
if (isPortraitMode())
{
int tmp = width;
//noinspection SuspiciousNameCombination
width = height;
height = tmp;
}
final int layoutWidth = right - left;
final int layoutHeight = bottom - top;
// Computes height and width for potentially doing fit width.
int childWidth = layoutWidth;
int childHeight = (int) (((float) layoutWidth / (float) width) * height);
for (int i = 0; i < getChildCount(); ++i)
{
getChildAt(i).layout(0, 0, childWidth, layoutHeight);
}
try
{
startIfReady();
}
catch (SecurityException se)
{
Log.e(TAG, "Do not have permission to start the camera", se);
}
catch (IOException e)
{
Log.e(TAG, "Could not start camera source.", e);
}
}
回答by Reinstate Monica
There are two ways to make the camera image fill the entire screen.
有两种方法可以使相机图像充满整个屏幕。
- Akesh Dubey's answer, which displays the whole image by stretching it to fit the layout's width and height. However, aspect ratio is not preserved.
- My answer below, which crops the image to make it fit without sacrificing aspect ratio.
- Akesh Dubey 的答案,它通过拉伸以适合布局的宽度和高度来显示整个图像。但是,不会保留纵横比。
- 我在下面的答案是裁剪图像以使其适合而不牺牲纵横比。
In order to crop-fit the image, all you have to do is change one >
into a <
. Find the below if-statement and change the condition like so:
为了裁剪,适应图像,所有你需要做的是改变一个>
成一个<
。找到下面的 if 语句并像这样更改条件:
if (childHeight < layoutHeight) {
childHeight = layoutHeight;
childWidth = (int)(((float) layoutHeight / (float) height) * width);
}