我正在做一個應用程序,它使用相機拍攝照片,然後旋轉並縮放它。 我需要旋轉圖像,因爲相機返回錯誤的旋轉圖像,我需要縮放它以減小其大小。 我首先將臨時目錄保存在相機返回的原始圖像中,然後我讀取它並進行修改,將新圖像保存到新文件中。 我嘗試過使用矩陣來旋轉和縮放圖片,但它失去了質量。 然後我試着用Bitmap.createScaledBitmap先縮放它,然後用矩陣旋轉它,但結果甚至比僅使用矩陣的結果更醜。 然後我試着先旋轉它,然後使用總是Bitmap.createScaledBitmap來調整它的大小。圖像不會丟失質量,但是在旋轉圖像後縮放它並將寬度和高度反轉後,圖像就會被拉伸。也嘗試根據所做的旋轉反轉高度和寬度,但它又失去了質量。 這是我寫的最後一段代碼:Android - 縮放,旋轉並將位圖保存到SD卡而不會丟失質量
in= new FileInputStream(tempDir+"/"+photo1_path);
out = new FileOutputStream(file+"/picture.png");
Bitmap src = BitmapFactory.decodeStream(in);
int iwidth = src.getWidth();
int iheight = src.getHeight();
int newWidth = 0;
int newHeight = 0;
newWidth = 800;
newHeight = 600;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth)/iwidth;
float scaleHeight = ((float) newHeight)/iheight;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
//matrix.postScale(scaleWidth, scaleHeight);
int orientation = getOrientation(MyActivity.this,Uri.parse(tempDir+"/"+photo1_path));
switch(orientation) {
case 3:
orientation = 180;
break;
case 6:
orientation = 90;
break;
case 8:
orientation = 270;
break;
}
int rotate = 0;
switch(orientation) {
case 90:
rotate=90;
break;
case 180:
rotate=180;
break;
case 270:
rotate=270;
break;
}
// rotate the Bitmap
matrix.postRotate(rotate);
src =Bitmap.createScaledBitmap(src , newWidth, newHeight, false);
// recreate the new Bitmap
Bitmap new_bit = Bitmap.createBitmap(src, 0, 0,
src.getWidth(), src.getHeight(), matrix, true);
new_bit.compress(Bitmap.CompressFormat.PNG, 100, out);
有什麼建議嗎?
編輯:如果我只旋轉或只縮放圖像,它不會失去質量。這是當我做這兩個圖像失去質量。另外,如果我在調整大小和縮放大小後將圖像放入ImageView中,它不會丟失質量,只是在將它保存到文件時丟失了質量。
問題與'Bitmap.CompressFormat.PNG'就是文件每次旋轉時都會變大。 –