我需要在创建图像之前缩放图像,并且只有当它超过1024KB时才会这样做(例如).
通过执行以下操作,我可以缩放图像,但我只需要缩放大于给定大小的图像.
Bitmap bmImg = null;
InputStream is = url.openStream();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 10;
bmImg = BitmapFactory.decodeStream(is,null,opts);
Run Code Online (Sandbox Code Playgroud)
如何获得位图的大小?(我很高兴知道字节数,而不是解压缩后的大小).
编辑:
我正在尝试这个:
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);
Run Code Online (Sandbox Code Playgroud)
我第一次使用InputStream(是)解码它与"inJustDecodeBounds"工作正常,我可以得到位图尺寸.问题是我第二次用它来实际解码图像,没有显示图像.
我究竟做错了什么?
我是个菜鸟所以我不能直接评论monkjack的回答.他的答案很慢的原因是它一次复制一个字节.使用偶数1K的缓冲区将显着提高性能.
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();
Run Code Online (Sandbox Code Playgroud)
您无法根据流的定义获取其大小。如果需要,您可以将流加载到字节数组中,从中获取字节数。您还可以使用 BitmapFactory 直接解码字节数组,这样效果会很好。
InputStream in = ...
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int i;
while ((i = in.read()) != -1) {
bos.write(i);
}
byte[] byteArray = bos.toByteArray();
if (byteArray.length > SOME_VALUE) {
// do things here
}
BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11480 次 |
| 最近记录: |