Search code examples
javaandroideclipseandroid-ndkjava-native-interface

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) . 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.


Solution

  • I know some method about image operation in android.

    Transform Drawable to Bitmap:

    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. Use getWidth() and getHeight() to get parameters of the image.

    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?

    If you get Uri, use 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:

    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.

    If you need Bitmap not BitmapDrawable , just return bitmap.

    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>