【问题标题】:Rotating picture from android camera来自android相机的旋转图片
【发布时间】:2014-04-28 17:19:14
【问题描述】:

我正在为相机设置旋转:

stopPreview();
Camera.Parameters p = mCamera.getParameters();
p.setRotation(90);
mCamera.setParameters(p);
startPreview();

照片是在设备处于水平方向时拍摄的。我需要将照片旋转到垂直方向。但是当我保存 jpeg 时,它永远不会旋转,它总是水平的。

protected void onJpegPicture(byte[] data, int width, int height) {
    saveJpeg(data, file);
}

我错过了什么吗?我认为我不应该在拍摄后手动旋转照片。我认为相机应该能够为我做到这一点。

setRotation 参数无关紧要。我尝试了所有可能的值(0、90、180、270)。

【问题讨论】:

    标签: android camera orientation android-camera


    【解决方案1】:

    假设你在调用 takePicture() 之前调用了 setParameters(),你的代码 sn-p 应该可以工作。您可以尝试在调用 takePicture 之前设置参数,而不是在开始预览之前设置参数,但这并不重要。

    请注意,您可能不想在此处使用 90,因为图片的方向是相对于传感器的方向确定的,而不是相对于您当前的 UI 方向。传感器是否与纵向或横向对齐取决于您的设备 - 您可以使用CameraInfo.orientation 进行检查。不过,如果您尝试了所有旋转值,您应该会看到效果。

    您在哪些设备上看到了这种行为?另外,您如何查看最终图像?一些图像查看器仍然不能正确解释 JPEG 旋转字段,尽管任何设备上的图库应用确实应该(尤其是如果它是默认应用)。

    【讨论】:

    • 我现在不需要正确的旋转,我想要旋转 - 这样我使用 90 作为旋转(并尝试了其他常量值)。这在索尼 xperia 上不起作用。我在互联网上发现这也是其他手机的常见问题。制造商不满足关于照片旋转的 android 要求。无论我的手机是水平还是垂直时拍照,无论我设置哪个旋转,我总是会以水平方向拍照。
    • 我们在较新的 Android 版本中为此添加了更好的测试,因此至少较新的设备不应再出现这些问题。
    【解决方案2】:

    尝试旋转两个相机并进行预览。即

    //rotate preview
    camera.setDisplayOrientation(90);
    //rotate camera
    Camera.Parameters p = camera.getParameters();
    p.setRotation(90);
    camera.setParameters(p);
    

    在几台 HTC 设备上测试,它可以工作。

    【讨论】:

    • 是的,它也适用于我的三星 Galaxy S4。是否依赖于设备?
    【解决方案3】:

    通过将Image 本身和rotation degree(在您的情况下为90)传递给它来使用此方法

    public static Image rotate(Image img, double angle){
        double sin = Math.abs(Math.sin(Math.toRadians(angle))), cos = Math.abs(Math.cos(Math.toRadians(angle)));
        int w = img.getWidth(null), h = img.getHeight(null);
        int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h
            * cos + w * sin);
        BufferedImage bimg = toBufferedImage(getEmptyImage(neww, newh));
        Graphics2D g = bimg.createGraphics();
        g.translate((neww - w) / 2, (newh - h) / 2);
        g.rotate(Math.toRadians(angle), w / 2, h / 2);
        g.drawRenderedImage(toBufferedImage(img), null);
        g.dispose();
        return toImage(bimg);
    }
    

    【讨论】:

    • 我不想手动执行此操作。将图像旋转 90 倍是一项简单的任务,因此所有能够旋转任意角度的方法都太复杂了。此外,安卓设备内存不足。我不想在内存中保留两个 img 副本(旋转前后)。我不想浪费时间从驱动器中保存和加载 img。
    • 没有解决问题的好方法。 setRotation 不保证图像旋转正确。您应该手动调整图像大小。
    猜你喜欢
    • 1970-01-01
    • 2014-04-19
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多