簡體   English   中英

如何調整輸入圖像的大小

[英]How to resize input image

如何在android中調整2張圖片的大小,因為一張圖片保持不變(可繪制文件夾中的.png圖片)應等於輸入圖片的大小(從用戶從移動圖片庫輸入的圖片)。 我在opencv中使用resize(src, dst)函數來調整兩個圖像的大小,在我也檢查這篇文章的同時,在java端對這種功能也不了解,但對我來說看起來有些額外的工作。

我知道一些有關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>
    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