0

アプリケーションで1つの画像ビットマップのサイズを変更すると、画質が低下するという問題があります。

サイズ変更のコードは次のとおりです。

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight,
            Config.ARGB_8888);

    float ratioX = newWidth / (float) bm.getWidth();
    float ratioY = newHeight / (float) bm.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.postScale(ratioX, ratioY, middleX, middleY);

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bm, middleX - bm.getWidth() / 2,
            middleY - bm.getHeight() / 2, paint);
    return scaledBitmap;
}

ビットマップのサイズを変更するための良い解決策はありますか?

4

1 に答える 1

0

このコードを使用してください。お役に立てば幸いです

    /***
 * This method is for aspect ratio means the image is set acc. to the aspect
 * ratio.
 * 
 * @param bmp
 *            bitmap passed
 * @param newWidth
 *            width you want to set for your image
 * @param newHeight
 *            hight you want to set for your image
 * @return bitmap
 */
public Bitmap resizeBitmap(Bitmap bmp, int newWidth, int newHeight) {
    Log.i(TAG,
            "height = " + bmp.getHeight() + "\nwidth = " + bmp.getWidth());
    if (bmp.getHeight() > newHeight || bmp.getWidth() > newWidth) {
        int originalWidth = bmp.getWidth();
        int originalHeight = bmp.getHeight();
        Log.i("TAG", "originalWidth = " + originalWidth
                + "\noriginalHeight = " + originalHeight);
        float inSampleSize;
        if (originalWidth > originalHeight) {
            inSampleSize = (float) newWidth / originalWidth;
        } else {
            inSampleSize = (float) newHeight / originalHeight;
        }
        newWidth = Math.round(originalWidth * inSampleSize);
        newHeight = Math.round(originalHeight * inSampleSize);
        Log.i("", "newWidth = " + newWidth + "\nnewHeight = " + newHeight);
        bitmap = Bitmap.createScaledBitmap(bmp, newWidth, newHeight, true);
    } else {
        bitmap = bmp;
        Log.i("", "bitmapWidth = " + bitmap.getWidth()
                + "\nbitmapHeight = " + bitmap.getHeight());
    }
    return bitmap;
}
于 2013-01-17T08:32:38.487 に答える