gpt4 book ai didi

android - 获取图像输入流的大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:59:57 25 4
gpt4 key购买 nike

我需要获取在输入流中找到的图像的高度和宽度。这是我所做的:

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;}

跳到问题所在:

它改变了 BitmapFactory.Options,即使我在下面的第二种方法计算后强制它为 false。

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

现在我的问题是还有另一种方法可以从输入流中获取图像的大小和宽度吗?我真的需要帮助,非常感谢任何帮助。

                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;
}
}

最佳答案

主要问题不在于 BitmapFactory.Options,而在于 InputStream。流是顺序的,因此当您读取流以仅解码图像的大小时,流中的指针会移动。接下来,当您再次调用 decode 以实际解码位图时,流指针不再位于位图的开头,并且您得到 null 因为部分流无法解码。

您需要重置流。根据该流是什么,您可以在其上使用 .mark.reset。本质上,你会做这样的事情:

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;
}

您可以通过调用 markSupported 来检查您的 InputStream 是否支持它。如果返回 true 那么您可以使用上述技术。如果它返回 false,则上述方法将不起作用,您实际上需要在解码完整位图之前关闭并重新打开流。

关于android - 获取图像输入流的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11213676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com