카메라에서 찍은 사진을 서버로 보내려고합니다. 그러나 그것은 1M 정도의 크기이고 나는 그것이 두 가지라고 생각합니다. 따라서 나는 사진의 크기를 줄이고 싶다. 아래의 방법을 사용하고 있습니다.
saveFile
- 단순히 파일에 비트 맵을 저장하고 경로를 반환하십시오.
calculateInSampleSize
은 사진 디멘션을 조정하도록 inSampleSize
을 반환합니다.
decodeSampledBitmapFromResource
은 파일에서 birmap을 디코딩하고 새로운 매개 변수로 다시 작성합니다.
Evrything은 좋지만 작동하지 않는 것 같습니다. small3.png의 Dimentions은 1240 * 768 * 24이며 크기는 여전히 1M입니다.사진 크기를 줄이는 방법?
나는
photo = saveFile(decodeSampledBitmapFromResource(picturePath,
800, 800),3);
방법
public String saveFile(Bitmap bm,int id) {
String file_path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Mk";
File dir = new File(file_path);
if (!dir.exists())
dir.mkdirs();
File file = new File(dir, "smaller" + id + ".png");
FileOutputStream fOut;
try {
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return file.getPath();
}
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;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/(float) reqHeight);
final int widthRatio = Math.round((float) width/(float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(String path,
int reqWidth, int reqHeight) {
Log.d("path", path);
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
원래 사진을 유지할 생각이 없습니다. – Yarh
괜찮습니까? 샘플 크기와 품질을 처리 할 수 있습니다. –