SurfaceView 上的人像 Android 相机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2543059/
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
Android Camera in Portrait on SurfaceView
提问by Prasanna
I tried several things to try to get the camera preview to show up in portrait on a SurfaceView
. Nothing worked. I am testing on a Droid that has 2.0.1. I tried:
我尝试了几种方法来尝试让相机预览以纵向显示在SurfaceView
. 没有任何效果。我正在测试具有 2.0.1 的 Droid。我试过:
1) forcing the layout to be portrait by: this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
1)通过以下方式强制布局为纵向: this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
2) using
2) 使用
Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
parameters.setRotation(90);
camera.setParameters(parameters);
Is there something else I can try? If this a bug in Android or the phone how can I make sure that this is the case so that I have proof to inform the client?
还有什么我可以尝试的吗?如果这是 Android 或手机中的错误,我如何确保是这种情况,以便我有证据通知客户?
Thanks, Prasanna
谢谢,普拉萨纳
回答by Ed Jellard
As of API lvl 8, this is available:
从 API lvl 8 开始,这是可用的:
public final void setDisplayOrientation (int degrees)
public final void setDisplayOrientation(整数度)
i.e. with portait in the manifest:
即在清单中带有 portait:
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
mCamera.setDisplayOrientation(90);
回答by valientinx
i have a working solution for portrait mode working in 2.1 (tested on Desire) maybe less.
我有一个适用于 2.1(在 Desire 上测试)的纵向模式的工作解决方案,可能更少。
Activity screen orientation is set to portrait. (android:screenOrientation="portrait")
活动屏幕方向设置为纵向。(android:screenOrientation="人像")
the camera parameters:
相机参数:
Camera.Parameters p = mCamera.getParameters();
Camera.Parameters p = mCamera.getParameters();
p.set("jpeg-quality", 100);
p.set("orientation", "landscape");
p.set("rotation", 90);
p.setPictureFormat(PixelFormat.JPEG);
p.setPreviewSize(h, w);// here w h are reversed
mCamera.setParameters(p);
and the image will be portrait.
并且图像将是纵向的。
SurfaceHolder you use for camera must be at a size compatible with phone preview size usualy screen resolution.
您用于相机的 SurfaceHolder 的尺寸必须与手机预览尺寸通常的屏幕分辨率兼容。
Funny on Desire 2.2 is not working... Here is the fix:
有趣的 Desire 2.2 不起作用......这是修复:
At surfaceCreated(..) or when you have this line
camera = Camera.open();
add
camera.setDisplayOrientation(90);//only 2.2>
Camera.Parameters p = camera.getParameters();
p.set("jpeg-quality", 100);
p.setRotation(90);
p.setPictureFormat(PixelFormat.JPEG);
p.setPreviewSize(h, w);
camera.setParameters(p);
回答by Mani
You can try this (good for 2.2 or below). Here I rotate the image before saving it to sd card. But it is only for portrait mode. If you had to make it for both mode then you should check camera orientation and put some check before capturing image.
你可以试试这个(适用于 2.2 或更低版本)。在这里,我在将图像保存到 SD 卡之前旋转了图像。但它仅适用于纵向模式。如果您必须在两种模式下都使用它,那么您应该检查相机方向并在捕获图像之前进行一些检查。
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
imageFilePath = getFilename();
InputStream is = new ByteArrayInputStream(data);
Bitmap bmp = BitmapFactory.decodeStream(is);
// Getting width & height of the given image.
if (bmp != null){
int w = bmp.getWidth();
int h = bmp.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(90);
// Rotating Bitmap
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
rotatedBMP.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
outStream = new FileOutputStream
(String.format(imageFilePath,System.currentTimeMillis()));
outStream.write(byteArray);
outStream.close();
} else {
outStream = new FileOutputStream
(String.format(imageFilePath,System.currentTimeMillis()));
outStream.write(data);
outStream.close();
}
preview.camera.startPreview();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
};
回答by Roman Nurik
There's no way to do this on many current devices, including the G1 and Droid. Take a look at the relevant bug report here:
目前的许多设备都无法做到这一点,包括 G1 和 Droid。在这里查看相关的错误报告:
Also see a comment from one of the Android engineers (Dave) here:
还可以在此处查看一位 Android 工程师 (Dave) 的评论:
回答by Brenden
The link Roman gave of the issue thread has a workable solution that I'm using now.
Roman给出的问题线程的链接有一个我现在正在使用的可行解决方案。
Find it here: http://code.google.com/p/android/issues/detail?id=1193#c26
在这里找到它:http: //code.google.com/p/android/issues/detail?id=1193#c26
回答by Nandagopal T
There is no need you have to set any parameters for the orientation until you need to do that explicitly. By Default, it supports this facility. In my case, i have a Activity and above that activity i have a camera view, so i didn't set any orientation for the camera properties, instead for the activity i set the orientation as portrait in the Manifest file. now the app looks and works good. Might be helpful for some one..
您无需为方向设置任何参数,直到您需要明确地这样做。默认情况下,它支持此功能。就我而言,我有一个活动,在该活动之上我有一个相机视图,所以我没有为相机属性设置任何方向,而是在清单文件中将活动方向设置为纵向。现在该应用程序看起来和运行良好。可能对某人有帮助..
Thanks.
谢谢。