-4
我有一个应用程序,您可以在其中拍照或从图库中选择图像。 一次拍摄/选择的照片必须上传到服务器。
我的问题是,因为有不同的设备具有不同的大小和分辨率,压缩的图像应该加载到服务器上的原始大小,还是应该放大并压缩?
为所有设备缩放图像的最佳方式是什么?压缩图像android
现在我使用这种算法,当用户通过相机或画廊
private void setPic() throws IOException {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, options);
// Calculate inSampleSize
int sampleSize = calculateInSampleSize(options, 720, 720);
Log.d("UserPanel", "Samplesize: " + sampleSize);
Log.d("SquareImage", ""+image.getWidth());
options.inSampleSize = sampleSize;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
options.inPreferredConfig= Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, options);
Log.d("UserPanel", "Bytebitmap: " + bitmap.getByteCount());
ExifInterface exif = new ExifInterface(mCurrentPhotoPath);
String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;
Matrix matrix = new Matrix();
matrix.setRotate(rotationAngle, (float) bitmap.getWidth()/2, (float) bitmap.getHeight()/2);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, options.outWidth, options.outHeight, matrix, true);
Log.d("UserPanel", "Bytebitmaprotate: " + rotatedBitmap.getByteCount());
MediaStore.Images.Media.insertImage(getContentResolver(), rotatedBitmap, imageFileName, "");
File fdelete = new File(mCurrentPhotoPath);
if (fdelete.delete()) Log.d("UserPanel", "Delete");
imageToServer = getStringImage(rotatedBitmap);
}
这是calculateSampleSize
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
Log.d("UserPanel", "Width: " + width + " height: " + height);
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height/2;
final int halfWidth = width/2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight/inSampleSize) >= reqHeight
&& (halfWidth/inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
要压缩图像拍摄照片我固定的大小100KB
private String getStringImage(Bitmap bmp) {
int compressQuality = 100;
int streamLength = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
do{
ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
Log.d("compressBitmap", "Quality: " + compressQuality);
bmp.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
byte[] bmpPicByteArray = bmpStream.toByteArray();
streamLength = bmpPicByteArray.length;
compressQuality -= 5;
Log.d("compressBitmap", "Size: " + streamLength/1024+" kb");
}while (streamLength >= 100000);
bmp.compress(Bitmap.CompressFormat.JPEG, compressQuality, baos);
byte[] imageBytes = baos.toByteArray();
Log.d("UserPanel", "Bytebitmap compress: " + imageBytes.length);
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}