在 Android 中设置相机图像大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3507771/
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
Setting camera image size in Android
提问by celestialorb
At the moment I am attempting to set my camera on my Motorola Droid phone to take an image that matches the size of my screen (854 x 480 pixels), and I am attempting to accomplish via the parameters for the camera as such:
目前,我正在尝试在我的摩托罗拉 Droid 手机上设置我的相机以拍摄与我的屏幕尺寸(854 x 480 像素)相匹配的图像,并且我试图通过相机的参数来完成:
Camera.Parameters parameters = this.mCamera.getParameters();
Log.i(TAG, "CAMERA SIZE: (" + this.mCameraView.getWidth() + ", " + this.mCameraView.getHeight() + ")");
parameters.setPictureSize(this.mCameraView.getWidth(), this.mCameraView.getHeight());
this.mCamera.setParameters(parameters);
this.mCamera.takePicture(null, null, this);
I have my activity implement the Camera.PictureCallback method of onPictureTaken (excluding log calls), so when the takePicture method is called it runs this method:
我让我的活动实现了 onPictureTaken 的 Camera.PictureCallback 方法(不包括日志调用),所以当调用 takePicture 方法时,它运行这个方法:
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap image = BitmapFactory.decodeByteArray(data, 0, data.length);
//Size imageSize = camera.getParameters().getPictureSize();
//image = Bitmap.createBitmap(image, 0, 0, imageSize.width, imageSize.height);
this.mCameraView.setBackgroundDrawable(new BitmapDrawable(image));
}
For some reason though, my camera is taking pictures in 1280 x 960. Is this some sort of minimum size the camera can capture an image in? From log calls I can see that the Camera's parameters are still set to have a picture size of 854 x 480, but image keeps coming out as 1280 x 960. Am I decoding the image incorrectly, am I setting the camera's parameters incorrectly, or am I doing something else wrong?
但出于某种原因,我的相机正在以 1280 x 960 的分辨率拍照。这是相机可以捕捉图像的某种最小尺寸吗?从日志调用中我可以看到相机的参数仍然设置为 854 x 480 的图片大小,但图像一直显示为 1280 x 960。我是否对图像解码错误,我是否设置了错误的相机参数,或者是我做错了什么?
Thanks in advance for any help you can give!
在此先感谢您提供的任何帮助!
Regards, celestialorb.
问候, celestialorb。
回答by nawab
Yes there are methods provided to get the picture sizes supported by camera. The method the paramters class of camera which provides the supported picture sizes by camera is getSupportedPictureSizes(); Using this method, you can get the supported size by camera. The code snippet would look like this:-
是的,提供了一些方法来获取相机支持的图片尺寸。提供相机支持图片尺寸的相机参数类的方法是getSupportedPictureSizes();使用此方法,您可以通过相机获取支持的尺寸。代码片段如下所示:-
Camera camera = Camera.open();
Parameters params = camera.getParameters();
List<Camera.Size> sizes = params.getSupportedPictureSizes();
I hope this will help you.
我希望这能帮到您。
Thanks Nawab
谢谢纳瓦布
回答by celestialorb
I have found the answer, apparently my own screen resolution is not a supported size for the camera. I was not aware of the camera only being able to support certain sizes as I am programming with the 1.6 SDK.
我找到了答案,显然我自己的屏幕分辨率不是相机支持的尺寸。我不知道相机只能支持某些尺寸,因为我正在使用 1.6 SDK 进行编程。
Is there any way to retrieve the supported sizes of the camera for 1.5/1.6?
有什么方法可以检索 1.5/1.6 支持的相机尺寸吗?