Android的:调整位图不失品质 [英] Android: Resizing Bitmaps without losing quality
问题描述
我真的寻遍了整个网络,然后再发布。我的问题是,我不能不失图像的质量(质量实在是太差和像素化)调整位图。
i've really searched through over the entire web before posting. My problem is that i cannot resize bitmap without losing the quality of the image (the quality is really bad and pixelated).
我把位图从相机,然后我必须为下限值,所以我可以把它上传到服务器快多了。
I take the bitmap from camera and then i have to downscale it, so i can upload it to the server much faster.
这是做采样功能
public Bitmap resizeBitmap(Bitmap bitmap){
Canvas canvas = new Canvas();
Bitmap resizedBitmap = null;
if (bitmap !=null) {
int h = bitmap.getHeight();
int w = bitmap.getWidth();
int newWidth=0;
int newHeight=0;
if(h>w){
newWidth = 600;
newHeight = 800;
}
if(w>h){
newWidth = 800;
newHeight = 600;
}
float scaleWidth = ((float) newWidth) / w;
float scaleHeight = ((float) newHeight) / h;
Matrix matrix = new Matrix();
// resize the bit map
matrix.preScale(scaleWidth, scaleHeight);
resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawBitmap(resizedBitmap, matrix, paint);
}
return resizedBitmap;
和我这是怎么从活动的结果所得到的图像
and this is how i get the image from activity result
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if(resultCode != RESULT_CANCELED){
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
getContentResolver().notifyChange(mImageUri, null);
ContentResolver cr = this.getContentResolver();
try
{
b = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);
Log.d("foto", Integer.toString(b.getWidth()));
Log.d("foto", Integer.toString(b.getHeight()));
addPhoto.setImageBitmap(b);
}
catch (Exception e)
{
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
Log.d("TAG", "Failed to load", e);
}
}
}
我开始认为,要得到一个小图片尺寸的最佳方式是设置摄像头分辨率。任何人都可以帮忙吗?
I'm starting to think that the best way to get a small picture size is to set the camera resolution. Anyone else can help?
推荐答案
尝试<一个href=\"http://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap%28android.graphics.Bitmap,%20int,%20int,%20boolean%29\"相对=nofollow> Bitmap.createScaledBitmap 。
它也有一个选项,以筛选源。
Try Bitmap.createScaledBitmap. It also has an option to filter the source.
这篇关于Android的:调整位图不失品质的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!