简体   繁体   English

从Android相机旋转图片

[英]Rotating picture from android camera

I'm setting rotation for camera: 我正在为相机设置旋转:

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

Photo is taken when device is in horizontal orientation. 当设备处于水平方向时拍摄照片。 I need photo rotated to vertical orientation. 我需要将照片旋转到垂直方向。 But when I'm saving jpeg it is never rotated, it's always horizontal. 但是,当我保存jpeg时,它永远不会旋转,它始终是水平的。

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

Am I missing something? 我想念什么吗? I don't think I should manually rotate picture after taking it. 我认为拍摄后不应该手动旋转图片。 I think camera should be able to do this for me. 我认为相机应该能够为我做到这一点。

setRotation parameter doesn't matter. setRotation参数无关紧要。 I tried all possible values (0, 90, 180, 270). 我尝试了所有可能的值(0、90、180、270)。

The code snippet you have should work, assuming you call setParameters() before calling takePicture(). 假设您在调用takePicture()之前先调用setParameters(),那么您拥有的代码段应该可以正常工作。 You could try setting the parameters right before your takePicture call, instead of before starting preview, but that should not matter. 您可以尝试在调用takePicture之前而不是在开始预览之前设置参数,但这无关紧要。

Note that you probably don't want to use 90 here, since the orientation of the picture is determined relative to the orientation of the sensor, not to your current UI orientation. 请注意,您可能不想在此处使用90,因为图片的方向是相对于传感器的方向而不是您当前的UI方向确定的。 And whether the sensor is aligned with portrait or landscape depends on your device - you can check this with CameraInfo.orientation . 传感器是纵向还是横向对齐取决于您的设备-您可以使用CameraInfo.orientation进行检查。 Nevertheless, you should see an effect if you've tried all the rotation values. 但是,如果尝试了所有旋转值,您应该会看到效果。

Which devices are you seeing this behavior on? 您在哪些设备上看到此行为? Also, how are you viewing the final image? 另外,您如何查看最终图像? Some image viewers still don't interpret the JPEG rotation field correctly, though any on-device gallery app really should (especially if it's the default application). 尽管任何设备上的图库应用程序确实应该(尽管特别是默认应用程序),但某些图像查看器仍然无法正确解释JPEG旋转字段。

Try rotating both camera and it's preview. 尝试同时旋转相机及其预览。 ie

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

Tested on several HTC devices and it works. 在多个HTC设备上进行了测试,并且可以正常工作。

Use this method by passing to it the Image itself and the rotation degree that is 90 in your case 通过将Image本身和您的情况下的rotation degree传递给它来使用此方法

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM