Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the size of an image inputstream

I need to get the height and width of the image found in the inputstream. Here is what I did:

private Boolean testSize(InputStream inputStream){
        BitmapFactory.Options Bitmp_Options = new BitmapFactory.Options();
        Bitmp_Options.inJustDecodeBounds = true;
        BitmapFactory.decodeResourceStream(getResources(), new TypedValue(), inputStream, new Rect(), Bitmp_Options);
        int currentImageHeight = Bitmp_Options.outHeight;
        int currentImageWidth = Bitmp_Options.outWidth;
        Bitmp_Options.inJustDecodeBounds = false;
    if(currentImageHeight < 200 || currentImageWidth < 200){
        Object obj = map.remove(pageCounter);
        Log.i("Page recycled", obj.toString());
        return true;
    }
    return false;}

Skipping to the problem at point:

It change BitmapFactory.Options even if I forced it to be false after calculation on my second method below.

private Bitmap getBitmap(InputStream InpStream){
    Bitmap originalBitmap = BitmapFactory.decodeStream(InpStream);//Null.
    return originalBitmap;
}

Now to my question is there another way of getting the size and width of an image from an inputstream? I really need help on this any help is greatly appreciated.

                ZipInputStream zip = null;
                zip = new ZipInputStream(new FileInputStream(getFileLocation()));
                for(ZipEntry zip_e = zip.getNextEntry(); zip_e != null ; zip_e = zip.getNextEntry()){
                    if(zip_e.isDirectory()) {
                        continue;
                    }
                    String file_zip = zip_e.getName();
                    String comparison = map.get(pageCounter).getHref();
                    if(file_zip.endsWith(comparison)){
                        SpannableString Spanable_String = new SpannableString("abc");
                        if(testSize(zip)){
                            map.remove(pageCounter);
                            return false;
                        }
                        Bitmap bitmap = getBitmap(zip);
                        if(bitmap == null){
                            map.remove(pageCounter);
                            return false;
                        }
                        image_page.put(zip_e.getName(), zip);
                        Drawable drawable_image = new FastBitmapDrawable(bitmap);
                        drawable_image.setBounds(0,0,drawable_image.getIntrinsicWidth(), drawable_image.getIntrinsicHeight());
                        ImageSpan imageSpan = new ImageSpan(drawable_image, ImageSpan.ALIGN_BASELINE);
                        Spanable_String.setSpan(imageSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        tv.setText(Spanable_String);
                        return false;
                    }
                }   
like image 467
Donkey King Avatar asked Dec 27 '22 22:12

Donkey King


1 Answers

The main problem is not with the BitmapFactory.Options, but with the InputStream. The stream is sequential, therefore when you read the stream to decode the image for size only, the pointer in the stream is moving. Next, when you're calling decode again to actually decode the bitmap, the stream pointer is no longer at the beginning of the bitmap, and you get null because the partial stream cannot be decode.

You would need to reset the stream. Depending on what that stream is, you might be able to use .mark and .reset on it. Essentially, you'd do something like this:

private Boolean testSize(InputStream inputStream) {
    BitmapFactory.Options Bitmp_Options = new BitmapFactory.Options();
    Bitmp_Options.inJustDecodeBounds = true;

    inputStream.mark(inputSteram.available());

    BitmapFactory.decodeResourceStream(getResources(), new TypedValue(), inputStream, new Rect(), Bitmp_Options);

    inputSteram.reset();

    int currentImageHeight = Bitmp_Options.outHeight;
    int currentImageWidth = Bitmp_Options.outWidth;
    Bitmp_Options.inJustDecodeBounds = false;

    if(currentImageHeight < 200 || currentImageWidth < 200){
        Object obj = map.remove(pageCounter);
        Log.i("Page recycled", obj.toString());
        return true;
    }

    return false;
}

You can check whether your InputStream supports this or not by calling markSupported on it. If that returns true then you can use the above technique. If it returns false, then the above method will not work and you will actually need to close and reopen the stream again before decoding the full bitmap.

like image 59
Aleks G Avatar answered Jan 12 '23 00:01

Aleks G