简体   繁体   English

如何调整输入图像的大小

[英]How to resize input image

How to resize 2 images in android , as one image which remain constant ( .png image in drawable folder) should be equal to the size of input image (image enter from the user from mobile gallery) . 如何在android中调整2张图片的大小,因为一张图片保持不变(可绘制文件夹中的.png图片)应等于输入图片的大小(从用户从移动图片库输入的图片)。 I use resize(src, dst) function in opencv for resizing two images , don't have any idea of this kind of function in java end as I check this post as well but it look some kind of extra loaded work to me. 我在opencv中使用resize(src, dst)函数来调整两个图像的大小,在我也检查这篇文章的同时,在java端对这种功能也不了解,但对我来说看起来有些额外的工作。

I know some method about image operation in android. 我知道一些有关android中图像操作的方法。

Transform Drawable to Bitmap: 将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;  
}

Resize 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;  
}

You can transform your first Drawable image to Bitmap, then resize it with the second method. 您可以将第一个Drawable图像转换为Bitmap,然后使用第二种方法调整其大小。 Use getWidth() and getHeight() to get parameters of the image. 使用getWidth()和getHeight()获取图像参数。

I don't know whether this is the best solution. 我不知道这是否是最佳解决方案。 If I didn't understand your intent well, make a comment and I can edit my answer. 如果我不太了解您的意图,请发表评论,然后编辑答案。

Edit: 编辑:

You can get Uri or the path of the image right? 您可以获取Uri或图像的路径正确吗?

If you get Uri, use String path = uri.getPath(); 如果您获得Uri,请使用String path = uri.getPath(); to get the path. 获得路径。

Then 然后

Decode Bitmap from file: 从文件解码位图:

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

If the size of image is not too big, load it directly wouldn't cause memory leaks, everything is OK. 如果图像的大小不是太大,直接加载它不会导致内存泄漏,一切正常。

But if you don't know the size, I recommend the next method. 但是,如果您不知道大小,我建议使用下一种方法。

Decode BitmapDrawable from 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);
}

This method will return a scaled BitmapDrawable object to prevent memory leaks. 此方法将返回缩放后的BitmapDrawable对象,以防止内存泄漏。

If you need Bitmap not BitmapDrawable , just return bitmap. 如果您需要Bitmap而不是BitmapDrawable ,则只需返回位图。

Edit2: EDIT2:

ThirdActivity.java : 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: 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>
    Bitmap bmpIn = BitmapFactory.decodeResource(view.getResources(), R.drawable.image);
    BitMap bmpOut = Bitmap.createScaledBitmap(bmpIn, width, height, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM