-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;
}