如何创建位图前,要知道从InputStream的位图的大小? [英] How to know a Bitmap size from InputStream before creating the Bitmap?

查看:202
本文介绍了如何创建位图前,要知道从InputStream的位图的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在创建它之前缩放图像,我想这样做,只有当它超过1024KB(例如)。

I need to scale an image before creating it and I want to do it only if it exceeds 1024KB (for example).

通过执行以下操作,我可以缩放图像,但我只需要扩展那些是大于给定大小。

By doing the following I can scale the image but I only need to scale the ones that are bigger than the given size.

Bitmap bmImg = null;
InputStream is = url.openStream();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 10;
bmImg = BitmapFactory.decodeStream(is,null,opts);

我怎样才能得到位图的大小? (我很高兴知道的字节数,而不是大小后DECOM pressing)。

How can I get the size of the Bitmap? (I'm happy knowing the amount of bytes, not the size after decompressing).

编辑:

我想这样的:

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
Bitmap bmImg=BitmapFactory.decodeStream(is,null,opts);
Log.e("optwidth",opts.outWidth+"");
Bitmap bmImg1 = BitmapFactory.decodeStream(is);

我第一次使用的InputStream(是)脱code它与inJustDe codeBounds工作正常,我可以得到位图的尺寸。 的问题是,第二次我用它实际上去code中的图像,没有图像被示出。

The first time I use the InputStream (is) to decode it with the "inJustDecodeBounds" works fine and I can get the Bitmap dimensions. The problem is that the second time I use it to actually decode the image, no image is shown.

我是什么做错了吗?

推荐答案

我是小白这样我就可以monkjack的回答直接发表评论。其原因他的回答是如此之慢是,它复制一次一个字节。使用连1K的缓冲区将显著提高性能。

I'm a noob so I can't comment on monkjack's answer directly. The reason his answer is so slow is that it's copying one byte at a time. Using a buffer of even 1K will significantly improve performance.

InputStream in = getContentResolver().openInputStream(docUri);

ByteArrayOutputStream bos = new ByteArrayOutputStream();
int i;
byte[] buffer = new byte[1024];
while ((i = in.read(buffer)) != -1) {
    bos.write(buffer);
}
byte[] docbuffer = bos.toByteArray();

这篇关于如何创建位图前,要知道从InputStream的位图的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆