- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我已经在我的应用程序中集成了 zxing 库。现在我希望 zxing 相机既可以在风景模式下工作,也可以在肖像模式下工作。现在它只在一种模式下工作。有人可以帮我解决这个问题吗?
最佳答案
我使用了 compyutech 的答案,但是缺少一些东西。所以,我将所有需要的东西都放在这里。
<强>1。相机配置管理器:在 initFromCameraParameters
方法中:
if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
camera.setDisplayOrientation(90);
}
也从相同的方法中删除此代码:
if (width < height) {
Log.i(TAG, "Display reports portrait orientation; assuming this is incorrect");
int temp = width;
width = height;
height = temp;
}
<强>2。 CameraManager : 类变量
private static final int MIN_FRAME_WIDTH = 340;
private static final int MIN_FRAME_HEIGHT = 240;
private static final int MAX_FRAME_WIDTH = 1200;
private static final int MAX_FRAME_HEIGHT = 675;
getFramingRect
方法:
public synchronized Rect getFramingRect() {
if (framingRect == null) {
if (camera == null) {
return null;
}
Point screenResolution = configManager.getScreenResolution();
if (screenResolution == null) {
// Called early, before init even finished
return null;
}
// Code added to enable portrait mode
int width = MIN_FRAME_WIDTH;
int height = MIN_FRAME_HEIGHT;
if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
int tmp = 7 * screenResolution.x / 8;
width = (tmp) < MIN_FRAME_WIDTH ? MIN_FRAME_WIDTH : (tmp);
tmp = 1 * screenResolution.y / 3;
height = (tmp) < MIN_FRAME_WIDTH ? MIN_FRAME_WIDTH : ((tmp) > MAX_FRAME_HEIGHT ? MAX_FRAME_HEIGHT : (tmp));
Log.d(TAG, "Customized code for portrait mode in getFramingRect executed (Piyush Merja) ");
}else{
// Original Code
width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
height = findDesiredDimensionInRange(screenResolution.y, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
}
// End
int leftOffset = (screenResolution.x - width) / 2;
int topOffset = (screenResolution.y - height) / 2;
framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);
Log.d(TAG, "Calculated framing rect: " + framingRect);
}
return framingRect;
}
private static int findDesiredDimensionInRange(int resolution, int hardMin, int hardMax) {
int dim = 5 * resolution / 8; // Target 5/8 of each dimension
if (dim < hardMin) {
return hardMin;
}
if (dim > hardMax) {
return hardMax;
}
return dim;
}
getFramingRectInPreview
方法:
public synchronized Rect getFramingRectInPreview() {
if (framingRectInPreview == null) {
Rect framingRect = getFramingRect();
if (framingRect == null) {
return null;
}
Rect rect = new Rect(framingRect);
Point cameraResolution = configManager.getCameraResolution();
Point screenResolution = configManager.getScreenResolution();
if (cameraResolution == null || screenResolution == null) {
// Called early, before init even finished
return null;
}
// Code added to enable portrait mode
if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
rect.left = rect.left * cameraResolution.y / screenResolution.x;
rect.right = rect.right * cameraResolution.y / screenResolution.x;
rect.top = rect.top * cameraResolution.x / screenResolution.y;
rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;
Log.d(TAG, "Customized code for portrait mode in getFramingRectInPreview executed (Piyush Merja) ");
}else{
// Original code commented
rect.left = rect.left * cameraResolution.x / screenResolution.x;
rect.right = rect.right * cameraResolution.x / screenResolution.x;
rect.top = rect.top * cameraResolution.y / screenResolution.y;
rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;
}
// End
framingRectInPreview = rect;//this was missing in compyutech's answer
}
return framingRectInPreview;
}
<强>3。解码处理程序:
在 decode
方法中:
long start = System.currentTimeMillis();
Result rawResult = null;
// Code added to enable portrait mode
if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
byte[] rotatedData = new byte[data.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++)
rotatedData[x * height + height - y - 1] = data[x + y * width];
}
data = rotatedData;
int tmp = width;
width = height;
height = tmp;
Log.d(TAG, "Customized code for portrait mode in decode executed (Piyush Merja) ");
}//end
PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);
<强>4。安卓 list 文件:
删除 CaptureActivity
的方向设置。
关于android - zxing 相机人像模式和风景在 android 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23403664/
我应该同时使用横向和纵向吗 @media only screen and (min-device-width: 320px) and (max-device-width: 480px) {} 或像这样
使用以下代码可以垂直预览相机,并且可以正常工作。但!!我得到一张风景照片! :( 如何垂直构建它?我有垂直预览 View ,但无法垂直保存图片。 问候和感谢,法兰 点击 public void onC
我知道如何仅为整个应用程序制作 iPad 应用程序肖像,以及如何仅制作特定 View 的肖像,但不知道如何仅制作启动图像肖像。我该怎么做? 目前,我只在 images.xcassets 中的“iOS
我需要一个 java 库来确定哪个图像是“人体”;这是一张“人脸”;这是一个“动物”;这是一个“风景”等等。 有这样的东西吗? 谢谢 最佳答案 我不认为那里有什么方便的东西。特别是因为你在这里有非常广
我是一名优秀的程序员,十分优秀!