简体   繁体   English

如何压缩Camera.takepicture拍摄的照片

[英]How to compress photo taken by Camera.takepicture

I'm developing an app where the user has the ability to take a picture through my CameraActivity (SurfaceView+Camera) and save it. 我正在开发一个应用程序,用户可以通过我的CameraActivity(SurfaceView + Camera)拍照并保存。 After that the bitmap will be processed. 之后,将处理位图。

Said that let me walk you through details. 说,让我向您详细介绍。 The picture is taken with: 图片拍摄:

mCamera.takePicture(null, null, new PictureCallback() {...}

I'm not specifying .jpeg quality. 我没有指定.jpeg质量。
Testing with HTC One S, the picture is taken and then saved. 使用HTC One S进行测试,然后拍摄照片并保存。 The picture size never exceeds 100kb 图片大小不得超过100kb
Testing with Samsung GT-I9000, the picture is taken and then saved but the difference here is the size, it exceeds 1,2Mb. 使用Samsung GT-I9000进行测试,图像被拍摄然后保存,但是这里的区别是大小,它超过1,2Mb。

Since I'll need to process the bitmap latter on I'm having some OutOfMemory issues with the GT-I9000 when I try to decode the bimap to process it: 由于我稍后需要处理位图,因此当我尝试对bimap进行解码以处理它时,GT-I9000遇到了内存不足的问题:

try {
    FileInputStream fis = new FileInputStream(new File(imagePath));
    Bitmap savedPicture = BitmapFactory.decodeStream(fis);


I've tried to change Camera Parameters .setJpegQuality(quality) but I it didn't made a huge difference. 我试图更改Camera Parameters .setJpegQuality(quality)但并没有太大的改变。

The image will occupy all the screen size and I used this approach Loading Large Bitmaps Efficiently It worked, I got rid of OutOfMemory Exceptions but still I've some questions: 图像将占据所有屏幕尺寸,并且我使用了这种方法高效地加载大型位图它有效,我摆脱了OutOfMemory异常,但仍然有一些问题:

  1. File size differences between devices? 设备之间的文件大小差异? I assume HTC One S is better than GT-I9000 so why doesn't save bigger pictures? 我认为HTC One S比GT-I9000好,为什么不保存更大的照片?

  2. Image resolution, HTC One S 640x480, GT-I9000 2560x1920 why? 图像分辨率为何,HTC One S 640x480,GT-I9000 2560x1920为什么? Well this answer the first question, but why is this happening? 好吧,这回答了第一个问题,但是为什么会这样呢?

Thanks for your time ;) 谢谢你的时间 ;)

Your problem is not with the image compression, but with resolution, ie size. 您的问题不是图像压缩,而是分辨率,即大小。 Different Android devices have different default Picture sizes. 不同的Android设备具有不同的默认图片尺寸。 But luckily, you have the Camera.Parameters.setPictureSize(int width, int height) method. 但幸运的是,您有Camera.Parameters.setPictureSize(int width,int height)方法。 You can set the size any time before you call Camera.takePicture() like this: 您可以在调用Camera.takePicture()之前随时设置大小,如下所示:

Camera.Parameters param = mCamera.getParameters();
param.setPictureSize(640, 480);
mCamera.setParameters(param);

Note that different Android devices support different picture sizes, too. 请注意,不同的Android设备也支持不同的图片尺寸。 Ie you cannot set an arbitrary size, you must choose one from the list specific to the device. 也就是说,您不能设置任意大小,必须从设备专用列表中选择一个。 There is a method Camera.Parameters.getSupportedPictureSizes(). 有一个Camera.Parameters.getSupportedPictureSizes()方法。

Note that the supported picture sizes on the same device are different for the two cameras (if the device has front facing camera and rear facing camera). 请注意,两个相机在同一设备上支持的图片大小是不同的(如果设备具有前置摄像头和后置摄像头)。 Open the correct camera before you start the queries and sets. 在开始查询和设置之前,请打开正确的摄像机。

Finally, I should confess that all devices I ever heard about, all supported 640x480 picture size. 最后,我应该承认我听说过的所有设备都支持640x480的图片尺寸。

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

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