如何调整输入图像的大小

时间:2014-09-22 20:27:46

标签: java android eclipse android-ndk java-native-interface

如何在android中调整2个图像的大小,因为一个保持不变的图像(可绘制文件夹中的.png图像)应该等于输入图像的大小(图像从用户从移动图库中输入)。我在opencv中使用resize(src, dst)函数来调整两个图像的大小,在java结束时我也不知道这种函数,因为我也检查this post但它看起来是某种额外加载的工作对我来说。

2 个答案:

答案 0 :(得分:1)

我知道一些关于android中图像操作的方法。

将Drawable转换为位图:

public static Bitmap drawableToBitmap(Drawable drawable) {  
    int width = drawable.getIntrinsicWidth();  
    int height = drawable.getIntrinsicHeight();  
    Bitmap bitmap = Bitmap.createBitmap(width, height, drawable  
            .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
            : Bitmap.Config.RGB_565);  
    Canvas canvas = new Canvas(bitmap);  
    drawable.setBounds(0, 0, width, height);  
    drawable.draw(canvas);  
    return bitmap;  
}

调整位图大小:

public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {  
    int width = bitmap.getWidth();  
    int height = bitmap.getHeight();  
    Matrix matrix = new Matrix();  
    float scaleWidht = ((float) w / width);  
    float scaleHeight = ((float) h / height);  
    matrix.postScale(scaleWidht, scaleHeight);  
    Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,  
            matrix, true);  
    return newbmp;  
}

您可以将第一个Drawable图像转换为Bitmap,然后使用第二种方法调整其大小。使用getWidth()和getHeight()来获取图像的参数。

我不知道这是否是最佳解决方案。如果我不太了解你的意图,请发表评论,我可以编辑我的答案。

修改

您可以获得Uri或图像的路径吗?

如果你得到Uri,请使用String path = uri.getPath();来获取路径。

然后

解码文件中的位图:

public static Bitmap getBitmap(String path) {
    return BitmapFactory.decodeFile(String path);
}

如果图像的大小不是太大,直接加载它不会导致内存泄漏,一切正常。

但如果您不知道尺寸,我建议采用下一种方法。

从路径解码BitmapDrawable:

public static BitmapDrawable getScaledDrawable(Activity a, String path) {
    Display display = a.getWindowManager().getDefaultDisplay();
    float destWidth = display.getWidth();
    float destHeight = display.getHeight();

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    float srcWidth = options.outWidth;
    float srcHeight = options.outHeight;

    int inSampleSize = 1;
    if (srcHeight > destHeight || srcWidth > destWidth) {
        if (srcWidth > srcHeight) {
            inSampleSize = Math.round(srcHeight / destHeight);
        } else {
            inSampleSize = Math.round(srcWidth / destWidth);
        }
    }

    options = new BitmapFactory.Options();
    options.inSampleSize = inSampleSize;

    Bitmap bitmap = BitmapFactory.decodeFile(path, options);
    return new BitmapDrawable(a.getResources(), bitmap);
}

此方法将返回一个缩放的BitmapDrawable对象,以防止内存泄漏。

如果您需要Bitmap而不是BitmapDrawable,请返回位图。

<强> EDIT2:

ThirdActivity.java:

public class ThirdActivity extends ActionBarActivity {

    private static final int REQUEST_IMAGE = 0;

    private Bitmap bitmapToResize;

    private Button mGetImageButton;
    private Button mResizeButton;
    private ImageView mImageViewForGallery;
    private ImageView mImageVIewForDrable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        mGetImageButton = (Button) findViewById(R.id.button_getImage);
        mGetImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // SET action AND miniType
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_PICK);
                intent.setType("image/*");
                // REQUEST Uri of image
                startActivityForResult(intent, REQUEST_IMAGE);
            }
        });

        mResizeButton = (Button) findViewById(R.id.button_resize);
        mResizeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resize();
            }
        });

        mImageViewForGallery = (ImageView) findViewById(R.id.imageView);

        mImageVIewForDrable = (ImageView) findViewById(R.id.imageViewFromDrable);
        mImageVIewForDrable.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) {return;}

        if (requestCode == REQUEST_IMAGE) {
            Uri uri = data.getData();
            // SET image
            mImageViewForGallery.setImageURI(uri);
            Drawable drawable = mImageViewForGallery.getDrawable();
            Log.e("asd", "Height:" + drawable.getIntrinsicHeight());
            Log.e("asd", "Width:" + drawable.getIntrinsicWidth());
        }
    }

    private void resize() {
        if (mImageViewForGallery.getDrawable() != null) {
            bitmapToResize = drawableToBitmap(mImageVIewForDrable.getDrawable());
            int width = mImageViewForGallery.getDrawable().getIntrinsicWidth();
            int height = mImageViewForGallery.getDrawable().getIntrinsicHeight();
            bitmapToResize = zoomBitmap(bitmapToResize, width, height);
            mImageVIewForDrable.setImageBitmap(bitmapToResize);
        } else {
            Log.e("asd", "setImageFirst");
        }

    }

    public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidht = ((float) w / width);
        float scaleHeight = ((float) h / height);
        matrix.postScale(scaleWidht, scaleHeight);
        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
                matrix, true);
        return newbmp;
    }

    public static Bitmap drawableToBitmap(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
                .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);
        return bitmap;
    }

}

activity_third.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="@android:color/darker_gray"
    tools:context="com.ch.summerrunner.ThirdActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/darker_gray">



            <Button
                android:id="@+id/button_getImage"
                android:text="@string/gallery"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <Button
                android:id="@+id/button_resize"
                android:text="@string/resize"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_toRightOf="@id/button_getImage"/>

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:background="@android:color/white"
                android:layout_below="@id/button_getImage"/>

            <ImageView
                android:id="@+id/imageViewFromDrable"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/white"
                android:layout_below="@id/imageView"/>
        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

答案 1 :(得分:0)

    Bitmap bmpIn = BitmapFactory.decodeResource(view.getResources(), R.drawable.image);
    BitMap bmpOut = Bitmap.createScaledBitmap(bmpIn, width, height, false);