Get the image as Thumbnail

后端 未结 2 966
-上瘾入骨i
-上瘾入骨i 2020-12-22 13:43

I am working in Android application but I\'m stuck with the problem, when I get the image from camera I am displaying that image in imageview but I need to show the same ima

相关标签:
2条回答
  • 2020-12-22 14:14

    Try this method will crate thumbnail of size you want will also maintain aspect ratio and scaling

    public Bitmap crateThumbNail(String imagePath,int size) {
            try {
                // Decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(imagePath, o);
                // The new size we want to scale to
                final int REQUIRED_SIZE = size;
    
                // Find the correct scale value. It should be the power of 2.
                int scale = 1;
                while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                    scale *= 2;
    
                // Decode with inSampleSize
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = scale;
                return BitmapFactory.decodeFile(imagePath, o2);
            } catch (Throwable e) {
                e.printStackTrace();
            }
            return null;
    
        }
    
    0 讨论(0)
  • 2020-12-22 14:38

    Use following method to get thumbnails.

    This method is useful when you have "Path" of image.

    /**
     * Create a thumb of given argument size
     * 
     * @param selectedImagePath
     *            : String value indicate path of Image
     * @param thumbWidth
     *            : Required width of Thumb
     * @param thumbHeight
     *            : required height of Thumb
     * @return Bitmap : Resultant bitmap
     */
    public static Bitmap createThumb(String selectedImagePath, int thumbWidth,
            int thumbHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
    
        // Decode weakReferenceBitmap with inSampleSize set
        options.inJustDecodeBounds = true;
    
        BitmapFactory.decodeFile(selectedImagePath, options);
    
        final int height = options.outHeight;
        final int width = options.outWidth;
    
        int inSampleSize = 1;
    
        if (height > thumbHeight || width > thumbWidth) {
            if (width > height) {
                inSampleSize = Math.round((float) height / (float) thumbHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) thumbWidth);
            }
        }
    
        options.inJustDecodeBounds = false;
        options.inSampleSize = inSampleSize;
    
        return BitmapFactory.decodeFile(selectedImagePath, options);
    }
    

    To use this method,

    createThumb("path of image",100,100);
    

    Edit

    This method is used when you have Bitmap of your image.

    public static Bitmap createThumb(Bitmap sourceBitmap, int thumbWidth,int thumbHeight) {
        return Bitmap.createScaledBitmap(sourceBitmap, thumbWidth, thumbHeight,true);
    }
    

    to use this method

    createThumb(editedImage, 100, 100);
    
    0 讨论(0)
提交回复
热议问题