我正在尝试在数据库中保存全尺寸图像,但该应用程序将图像保存的尺寸比用相机拍摄的原始图像最小。这是onActivityResult
方法:
case TAKE_AVATAR_CAMERA_REQUEST_DOCUMENTO:
case TAKE_AVATAR_CAMERA_REQUEST_INFRACCION:
if (resultCode == Activity.RESULT_CANCELED) {
// Avatar camera mode was canceled.
} else if (resultCode == Activity.RESULT_OK) {
// Took a picture, use the downsized camera image provided by
// default
Bitmap cameraPic = (Bitmap) data.getExtras().get("data");
if (cameraPic != null) {
try {
saveAvatar(cameraPic, requestCode);
} catch (Exception e) {
Log.e(DEBUG_TAG, "saveAvatar() with camera image failed.", e);
}
}
}
break;
private void saveAvatar(Bitmap avatar, int pRequestCode) {
String strAvatarFilename = "avatar.jpg";
String sPreferenceFoto = GAME_PREFERENCES_AVATAR;
String sNumeroActa = (new ActaConstatacionRules(this)).getNextNumeroActa();
int _idButton = 0;
switch (pRequestCode) {
case TAKE_AVATAR_CAMERA_REQUEST_LICENCIA:
strAvatarFilename = sNumeroActa + "_licencia.jpg";
sPreferenceFoto = CURRENT_ACTA_FOTO_LICENCIA;
_idButton = R.id.ImageButton_Licencia;
break;
case TAKE_AVATAR_CAMERA_REQUEST_DOCUMENTO:
strAvatarFilename = sNumeroActa + "_documento.jpg";
sPreferenceFoto = CURRENT_ACTA_FOTO_DOCUMENTO;
_idButton = R.id.ImageButton_Documento;
break;
case TAKE_AVATAR_CAMERA_REQUEST_INFRACCION:
strAvatarFilename = sNumeroActa + "_infraccion.jpg";
sPreferenceFoto = CURRENT_ACTA_FOTO_INFRACCION;
_idButton = R.id.ImageButton_Infraccion;
break;
default:
Utilities.ShowToast(this, "Seleccion de Imagen Invalida");
return;
}
File image = null;
try {
File sdCardDirectory = Environment.getExternalStorageDirectory();
image = new File(sdCardDirectory, strAvatarFilename);
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
avatar.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
Log.e(DEBUG_TAG, "Avatar compression and save failed.", e);
}
strAvatarFilename));
if (image == null)
return;
Uri imageUriToSaveCameraImageTo = Uri.fromFile(image);
Editor editor = mCurrenActaSettings.edit();
editor.putString(sPreferenceFoto, imageUriToSaveCameraImageTo.getPath());
editor.commit();
// Update the settings screen
ImageButton avatarButton = (ImageButton) findViewById(_idButton);
String strAvatarUri = mCurrenActaSettings.getString(sPreferenceFoto, RESOURCE_SIN_FOTO);
Uri imageUri = Uri.parse(strAvatarUri);
avatarButton.setImageURI(null); // Workaround for refreshing an
// ImageButton, which tries to cache the
// previous image Uri. Passing null
// effectively resets it.
avatarButton.setImageURI(imageUri);
}
我需要知道如何保存(例如)600 x 600的图像尺寸,但又不损失质量,现在可以保存200 x 100,但我不知道为什么。
答案 0 :(得分:0)
public File saveBitmapToFile(File file) {
try {
// BitmapFactory options to downsize the image
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inSampleSize = 6;
// factor of downsizing the image
FileInputStream inputStream = new FileInputStream(file);
//Bitmap selectedBitmap = null;
BitmapFactory.decodeStream(inputStream, null, options);
inputStream.close();
inputStream = null;
int originalWidth = options.outWidth;
int originalHeight = options.outHeight;
if (originalWidth > 0) {
int reqWidth = 600;
int reqHeight = (reqWidth * originalHeight) / originalWidth;
if (reqHeight >= 600)
reqHeight = 600;
Log.d("new image ", "getDropboxIMGSize: " + reqHeight + " " +
reqWidth);
// decode full image pre-resized
inputStream = new FileInputStream(file);
options = new BitmapFactory.Options();
// calc rought re-size (this is no exact resize)
options.inSampleSize = Math.max(originalWidth / reqWidth, originalHeight
/ reqHeight);
// decode full image
Bitmap roughBitmap = BitmapFactory.decodeStream(inputStream, null,
options);
// calc exact destination size
Matrix m = new Matrix();
RectF inRect = new RectF(0, 0, roughBitmap.getWidth(),
roughBitmap.getHeight());
RectF outRect = new RectF(0, 0, reqWidth, reqHeight);
m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER);
float[] values = new float[9];
m.getValues(values);
// resize bitmap
Bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap,
(int) (roughBitmap.getWidth()*values[0]),
(int) (roughBitmap.getHeight()*values[4]),true);
// override resized bitmap image
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 70, out);
}
return file;
} catch (IOException e) {
Log.e("Image", e.getMessage(), e);
return null;
}
}