我有应用程序捕获图像并使用重叠图像保存它。放置在捕获图像上方的图像就像是中心有透明部分的边框。问题是在某些设备上图片被破坏并且看起来像这样
所以棕色边框没问题,但拍摄的图像不合适。具体来说,它在HTC sense 3.6(Android 4.0.3)上再现 起初我捕获图像并创建带边框的位图。比我把它保存在SD卡上并在下一个活动中显示它。这是我的代码:
private class CameraPictureCallback implements Camera.PictureCallback {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = PictureStorage.getOutputMediaFile(PictureStorage.MEDIA_TYPE_IMAGE);
if (pictureFile == null) {
Logger.getInstance().log("Error creating media file, check storage permissions: ");
return;
}
if (data != null) {
Bitmap border = BitmapFactory.decodeResource(getResources(), R.drawable.photo_frame);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 4;
Bitmap origin_bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
Matrix matrix = new Matrix();
matrix.postRotate(mRotation);
//Border is 640x640px
Bitmap bitmap = Bitmap.createScaledBitmap(origin_bitmap, border.getWidth(), border.getHeight(), false);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
Bitmap resultBitmap = Bitmap.createBitmap(border.getWidth(), border.getHeight(), Bitmap.Config.ARGB_8888);
Canvas s = new Canvas(resultBitmap);
s.drawBitmap(bitmap, 0f, 0f, null);
s.drawBitmap(border, 0f, 0f, null);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
if (resultBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos)) {
fos.close();
}
resultBitmap.recycle();
bitmap.recycle();
border.recycle();
origin_bitmap.recycle();
Intent i = new Intent(getContext(), PictureConfirmActivity.class);
i.putExtra("filename", pictureFile.getPath());
getContext().startActivity(i);
} catch (FileNotFoundException e) {
Logger.getInstance().log(e);
} catch (IOException e) {
Logger.getInstance().log(e);
}
}
}
}
它不会在我的任何设备上重现,但问题非常实际,我需要解决它。我可以想象它可以在这里,resultBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos)
,但它只是一个想法
答案 0 :(得分:0)
问题在于设置正确的预览尺寸。我选择了第一个可用的屏幕尺寸,导致这种不可预测的结果。所以正确的解决方案是找到合适的预览尺寸
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
//some code before...
Camera.Parameters parameters = mCamera.getParameters();
Camera.Size previewSize = getBestPreviewSize(w, h, parameters);
parameters.setPreviewSize(previewSize.width, previewSize.height);
mCamera.setParameters(parameters);
}
private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters) {
Camera.Size result = null;
for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
if (size.width <= width && size.height <= height) {
if (result == null) {
result = size;
}
else {
int resultArea = result.width * result.height;
int newArea = size.width * size.height;
if (newArea > resultArea) {
result = size;
}
}
}
}
if (result == null) {
result = parameters.getSupportedPreviewSizes().get(0);
}
return result;
}