我正在尝试使用 Android 相机拍照。我需要捕获 1600(宽)x 1200(高)图像(第 3 方供应商要求)。我的代码似乎适用于许多手机摄像头,但 setPictureSize 会导致某些手机(三星 Galaxy S4、三星 Galaxy Note)崩溃,并导致其他手机(Nexus 7 平板电脑)上出现条纹图片。至少在 Nexus 上,我想要的尺寸显示在 getSupportPictureSizes 列表中。
我尝试过指定方向,但没有帮助。使用默认图片尺寸拍摄照片效果很好。
下面是一个裸奔的例子:
对于我的图像捕获,我需要 1600x1200、jpg、30% 压缩,因此我捕获的是 JPG 文件。
我想我有三个选择:
1) 弄清楚如何捕获 1600x1200 尺寸而不发生崩溃或裸奔,或者
2) 弄清楚如何将默认图片尺寸更改为 1600x1200 的 JPG。
3)目前我不知道的其他事情。
我发现其他一些帖子也有类似的问题,但不完全相同。我已经进入尝试的第二天,但没有找到解决方案。这是一篇接近的帖子:
相机图片转换为位图会导致图像混乱 https://stackoverflow.com/questions/15404547/camera-picture-to-bitmap-results-in-messed-up-image(所有建议都没有帮助我)
这是我的代码部分,在我遇到 S4/Note/Nexus 7 之前一直运行良好。我现在添加了一些调试代码:
Camera.Parameters parameters = mCamera.getParameters();
Camera.Size size = getBestPreviewSize(width, height, parameters);
if (size != null) {
int pictureWidth = 1600;
int pictureHeight = 1200;
// testing
Camera.Size test = parameters.getPictureSize();
List<Camera.Size> testSizes = parameters.getSupportedPictureSizes();
for ( int i = 0; i < testSizes.size(); i++ ) {
test = testSizes.get(i);
}
test = testSizes.get(3);
// get(3) is 1600 x 1200
pictureWidth = test.width;
pictureHeight = test.height;
parameters.setPictureFormat(ImageFormat.JPEG);
parameters.setPictureSize(pictureWidth, pictureHeight);
parameters.setJpegQuality(30);
parameters.setPreviewSize(size.width, size.height);
// catch any exception
try {
// make sure the preview is stopped
mCamera.stopPreview();
mCamera.setParameters(parameters);
didConfig = true;
catch(Exception e) {
// some error presentation was removed for brevity
// since didConfig not set to TRUE it will fail gracefully
}
}
这是我保存 JPG 文件的代码部分:
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
if ( data.length > 0 ) {
String fileName = "image.jpg";
File file = new File(getFilesDir(), fileName);
String filePath = file.getAbsolutePath();
boolean goodWrite = false;
try {
OutputStream os = new FileOutputStream(file);
os.write(data);
os.close();
goodWrite = true;
} catch (IOException e) {
goodWrite = false;
}
if ( goodWrite ) {
// go on to the Preview
} else {
// TODO return an error to the calling activity
}
}
Log.d(TAG, "onPictureTaken - jpeg");
}
};
任何关于如何正确设置拍照参数或如何裁剪或调整生成的照片大小的建议都会很棒。特别是如果它可以与旧相机(API 级别 8 或更高版本)一起使用!基于需要图片的完整宽度,我只能裁剪掉顶部。
Thanks!
编辑:这就是我最终所做的:
我首先处理 Camera.Parameters getSupportedPictureSizes,以使用第一个高度和宽度均大于我所需尺寸且宽度与高度比相同的参数。我将相机参数设置为该图片尺寸。
然后拍照后:
BitmapFactory.Options options = new BitmapFactory.Options();;
options.inPurgeable = true;
// convert the byte array to a bitmap, taking care to allow for garbage collection
Bitmap original = BitmapFactory.decodeByteArray(input , 0, input.length, options);
// resize the bitmap to my desired scale
Bitmap resized = Bitmap.createScaledBitmap(original, 1600, 1200, true);
// create a new byte array and output the bitmap to a compressed JPG
ByteArrayOutputStream blob = new ByteArrayOutputStream();
resized.compress(Bitmap.CompressFormat.JPEG, 30, blob);
// recycle the memory since bitmaps seem to have slightly different garbage collection
original.recycle();
resized.recycle();
byte[] desired = blob.toByteArray();
然后我将所需的 jpg 写入文件中以供上传。