首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android人像模式下的Zxing摄像头

Android人像模式下的Zxing摄像头
EN

Stack Overflow用户
提问于 2013-04-27 21:38:29
回答 9查看 46.2K关注 0票数 42

我想在Zxing的相机上显示portrait方向。

如何做到这一点?

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2013-04-27 21:54:21

下面是它的工作原理。

第一步:在( data data,int width,int height)中,添加以下行旋转数据之前的数据

DecodeHandler.java:

代码语言:javascript
运行
复制
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];
}
int tmp = width;
width = height;
height = tmp;

PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(rotatedData, width, height);

第二步:修改getFramingRectInPreview()**.**

CameraManager.java

代码语言:javascript
运行
复制
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;

步骤3:在 initFromCameraParameters(...)中禁用横向模式检查

CameraConfigurationManager.java

代码语言:javascript
运行
复制
//remove the following
if (width < height) {
  Log.i(TAG, "Display reports portrait orientation; assuming this is incorrect");
  int temp = width;
  width = height;
  height = temp;
}

第四步:在**setDesiredCameraParameters(...)**中添加以下代码行旋转摄像头

CameraConfigurationManager.java

代码语言:javascript
运行
复制
camera.setDisplayOrientation(90);

第5步:不要忘记将活动的方向设置为纵向。即:清单

票数 108
EN

Stack Overflow用户

发布于 2015-02-07 04:34:24

要支持所有方向并在旋转活动时自动更改,只需修改CameraManager.javaclass.即可

并从CaptureActivity.java中删除此方法getCurrentOrientation()

CameraManager.java中的创建此变量:

代码语言:javascript
运行
复制
int resultOrientation;

将此代码添加到openDriver(..)方法:

代码语言:javascript
运行
复制
setCameraDisplayOrientation(context, Camera.CameraInfo.CAMERA_FACING_BACK, theCamera);//this can be set after camera.setPreviewDisplay(); in api13+.

*创建此方法*链接:http://developer.android.com/reference/android/hardware/Camera.html

代码语言:javascript
运行
复制
public static void setCameraDisplayOrientation(Context context,int cameraId, android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int degrees = 0;
    switch (display.getRotation()) {
    case Surface.ROTATION_0: degrees = 0; break;
    case Surface.ROTATION_90: degrees = 90; break;
    case Surface.ROTATION_180: degrees = 180; break;
    case Surface.ROTATION_270: degrees = 270; break;
    }


    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        resultOrientation = (info.orientation + degrees) % 360;
        resultOrientation = (360 - resultOrientation) % 360;  // compensate the mirror
    } else {  // back-facing
        resultOrientation = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(resultOrientation);
}

*现在修改getFramingRectInPreview()*

代码语言:javascript
运行
复制
if(resultOrientation == 180 || resultOrientation == 0){//to work with landScape and reverse landScape
            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;
        }else{
            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;
        }

并将此方法修改为公共PlanarYUVLuminanceSource buildLuminanceSource(..)

代码语言:javascript
运行
复制
if(resultOrientation == 180 || resultOrientation == 0){//TODO: This is to use camera in landScape mode
        // Go ahead and assume it's YUV rather than die.
        return new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top, rect.width(), rect.height(), false);
    }else{
        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];
        }
        int tmp = width;
        width = height;
        height = tmp;
        return new PlanarYUVLuminanceSource(rotatedData, width, height, rect.left, rect.top, rect.width(), rect.height(), false);
    }
票数 7
EN

Stack Overflow用户

发布于 2013-09-06 11:59:44

您可以使用我的zxlib https://github.com/rusfearuth/zxing-lib-without-landscape-only分支。我只禁用了横向模式。您可以设置横向/纵向和查看正确的相机视图。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16252791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档