重新调整我的图像大小而不模糊

时间:2016-03-16 11:24:19

标签: android

我正在制作一个应用程序,通过使用意图捕获图像并根据我重新调整图像尺寸,但在捕获后它很模糊,所以我需要提出建议和帮助。 实际上我正在按比例重新调整图片大小"创建缩放位图"并希望隐藏小尺寸的图像,但在重新调整大小后会模糊不清。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {
        try {
            if (requestCode == 1) {
                if (data != null && data.getExtras() != null && data.getExtras().get("data") != null) {
                    bitmap = (Bitmap) data.getExtras().get("data");

                    int width = bitmap.getWidth();
                    int height = bitmap.getHeight();
                    int newWidth = 100;
                    int newHeight = 50;
                    float scaleWidth = ((float) newWidth) / width;
                    float scaleHeight = ((float) newHeight) / height;

                    Matrix matrix = new Matrix();
                    // resize the bit map
                    matrix.postScale(scaleWidth, scaleHeight);
                    // rotate the Bitmap

                    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

                    int h = resizedBitmap.getHeight();

                    int b = resizedBitmap.getWidth();
                    Log.e("height & width ", h + "" + b);

                    Bitmap resized = Bitmap.createScaledBitmap(bitmap, 25, 25, true);

                    int h1 = bitmap.getHeight();
                    int b1 = bitmap.getWidth();
                    Log.e("height 1  & width  1", h1 + "" + b1);

                    mbPhotoImageViewId.setImageBitmap(resized);
                    int size = resized.getWidth() * resized.getHeight();
                    ByteArrayOutputStream stream = new ByteArrayOutputStream(size);
                    resized.compress(Bitmap.CompressFormat.PNG, 100, stream);

                    byte[] image = stream.toByteArray();
                    System.out.println("byte array:" + image.length + "   " + image);

                    img_str = Base64.encodeToString(image, Base64.DEFAULT);
                    System.out.println("string: " + img_str.length() + "   " + img_str);

                    Log.d("BITMAP form of image", image.toString());

                }
            }
        } catch (Exception e) {
            e.toString();
            Toast.makeText(getApplicationContext(), " Error Code : C_M09004", Toast.LENGTH_LONG).show();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在这段代码中你必须根据这个高度和宽度传递你想要的高度和宽度这个函数调整大小图像

public Bitmap Resizedoesnotlosequality(String tempDir, int reqwid,
        int reqhei) {

    Bitmap src = null;

    try {
        BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        bmpFactoryOptions.inJustDecodeBounds = true;

        Bitmap bm = BitmapFactory.decodeFile(tempDir, bmpFactoryOptions);

        BitmapFactory.Options Options = new BitmapFactory.Options();
        Options.inJustDecodeBounds = false;
        Options.inSampleSize = calculateInSampleSize(bmpFactoryOptions,
                reqwid, reqhei);
        bm = BitmapFactory.decodeFile(tempDir, Options);

        Matrix matrix = new Matrix();
        // resize the bit map

        // recreate the new Bitmap
        bmpwid = bm.getWidth();
        bmphei = bm.getHeight();
        src = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(),
                matrix, true);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return src;
}

public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        while ((height / inSampleSize) > reqHeight
                || (width / inSampleSize) > reqWidth) {
            inSampleSize++;
        }
    }

    return inSampleSize;

}