简体   繁体   English

如何实用地减少android中图像的宽度和高度?

[英]How can i decrease the width and height for a image in android pragmatically?

My requirement is what ever the size(width,height not file size) image stored in the database i need to retrieving that as simple passport size of image. 我的要求是存储在数据库中的图像大小(宽度,高度而不是文件大小)是什么,我需要将其检索为图像的简单护照大小。

My code is : 我的代码是:

byte[] imgByte=null; 

in the oncreate method 在oncreate方法中

imageview=new ImageView(this);
imageview.setLayoutParams(llp2);
imgByte=cursor.getBlob(cursor.getColumnIndex("imagestore"));
imageview.setScaleType(ScaleType.CENTER);
imageview.setImageBitmap(BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length));
layout.addView(imageview)

when i displaying that displayed only what ever the size before enter. 当我显示时,只显示输入之前的尺寸。 so i need to fit that image size in the android. 所以我需要在Android中适应该图像大小。

I tried these links 我尝试了这些链接

Reduce size of Bitmap to some specified pixel in Android 在Android中将位图的大小减小到某些指定像素

but the problem here is i got image in byte. 但是这里的问题是我得到了字节图像。 But all codes image data type int only. 但是所有代码图像数据类型仅是int。 can i get the correct solution to decrease the image size pragmatically? 我可以找到正确的解决方案以实用地减小图像尺寸吗?

You can use following code to create sample sized image. 您可以使用以下代码创建样本大小的图像。 This will return Bitmap . 这将返回Bitmap

public static Bitmap decodeSampledBitmapFromResource(Resources res,
        int resId, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
} 

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

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

        // Calculate ratios of height and width to requested height and
        // width
        final int heightRatio = Math.round((float) height
                / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will
        // guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

Use this code, 使用此代码,

decodeSampledBitmapFromResource(getResources(),R.drawable.xyz, 100, 100);

Here, 100 * 100 sample size you are providing. 在这里,您提供的样本大小为100 * 100。 So Thumbnail of such size will be created. 因此将创建这种大小的缩略图。

Edit 编辑

To use bytes[] in code, use following short of code. 要在代码中使用bytes [],请使用以下简短代码。

public static Bitmap decodeSampledBitmap(byte[] data, int reqWidth,
        int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(data, 0, data.length, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeByteArray(data, 0, data.length, options);
}

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

相关问题 如何在android中实用地设置图像的layout_width - How to set layout_width of an image pragmatically in android 如何减少linearlayout的高度,这是android中的图像 - how to decrease height of linearlayout which is an image in android 如何在android中运行时增加或减少scrollview的高度和宽度? - how to increase or decrease Height and width of scrollview at runtime in android? 如何使用Android应用程序中的按钮增加/减少imageView的宽度和高度? - how to increase/decrease imageView width and height with buttons in android app? 如何在实用中删除android中的Chrome缓存 - How can I remove chrome cache in android pragmatically 如何添加以及在Android中实际删除布局? - How can i add as well as remove layout pragmatically in android? 我可以从android中的uri获取图像文件的宽度和高度吗? - can i get image file width and height from uri in android? 如何在Android中实用地根据图片大小设置布局宽度? - How to Set width of Layout according to the picture size pragmatically in android? 如何修复android平板电脑的宽度和高度 - How can I fix android tablet's width and height 如何在android中读取屏幕宽度和高度? - how can i read the screen width and height in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM