gpt4 book ai didi

java - 这是解码 InputStream 时 BitmapFactory 的错误吗?

转载 作者:行者123 更新时间:2023-11-30 04:44:21 26 4
gpt4 key购买 nike

这次我用的是这段代码。我想知道这次出了什么问题?我正在使用从网络加载图像的代码。如你看到的。但是图像在解码为位图时失去了质量。(可能是我错了)。你可以看看这张图片。 enter image description here

我试过了

  1. 从 URL 获取图像并将其保存为可绘制的 .jpg 图像。然后从资源加载图像。这次效果很好。 但是
  2. 当我从 URL 而不是资源中获取 Bitmap 时,它会变得更加模糊,您可以从上方检查它。

看起来 BitmapFectory 中可能有问题要删除

public class MurgeActivity extends Activity {
String abcc = "http://c3.ac-images.myspacecdn.com/images02/84/s_48e1fd15504c49ed8fd008f8592f3082.jpg";
String abcd = "http://c1.ac-images.myspacecdn.com/images02/92/s_3bf09d34200c4423a2b8cb73f12675b8.jpg";

@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ImageView img1 = (ImageView) findViewById(R.id.cus_image);
ImageView img4 = (ImageView) findViewById(R.id.cus_image4);

// Here I am getting image from net.
Bitmap map = getImageFromWeb("http://c3.ac-images.myspacecdn.com/images02/84/s_48e1fd15504c49ed8fd008f8592f3082.jpg");

// Here I am getting image from resources
//Bitmap map = BitmapFactory.decodeResource(getResources(),R.drawable.a1);

Bitmap enlarge = BitmapFactory.decodeResource(getResources(), R.drawable.enlarge_image_button);
img1.setImageBitmap(map);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
map.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();

String encodedImage = Base64.encodeBytes(b);
String items = combineArticleImage(encodedImage, enlarge);
String imgTag = "<img width='100' src='data:image/png;base64," + items
+ "' align='left' style='padding-right:5px; padding-bottom:5px'/>";

WebView webView = (WebView) findViewById(R.id.web);
webView.setBackgroundColor(R.color.black);
webView.loadData(imgTag, "text/html", "utf-8");

byte[] decodedString = Base64.decode(items, android.util.Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
Drawable end = new BitmapDrawable(decodedByte);

Drawable drb = Utils.combineSlidshowImages(new BitmapDrawable(map).getCurrent(), enlarge);
img4.setImageDrawable(drb);
} catch (Exception e) {
Log.e("Exeption", this.getClass().getSimpleName() + e.toString());
} catch (Error error) {
Log.e("Error", this.getClass().getSimpleName() + error.toString());
}

}

public static String combineArticleImage(String baseImage, Bitmap frontImage) {

String base64Image = null;
byte[] imagesBytes = null;
try {
imagesBytes = Base64.decode(baseImage);

if (imagesBytes != null) {


Bitmap map = BitmapFactory.decodeByteArray(imagesBytes, 0, imagesBytes.length);

Bitmap icon = frontImage;
Bitmap finalImag = combineImages(map, icon);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
finalImag.compress(Bitmap.CompressFormat.JPEG, 100, baos);

byte[] b = baos.toByteArray();
base64Image = Base64.encodeBytes(b);
}
} catch (Exception e) {
Log.e("Error", "Combining Article Image");
}
return base64Image;
}

public static Bitmap getImageFromWeb(String imglink) {
Bitmap bmpImage = null;
try {

URL imgURL = new URL(imglink);
URLConnection conn = imgURL.openConnection();
conn.connect();

InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);

bmpImage = BitmapFactory.decodeStream(bis);
bis.close();
is.close();

} catch (Exception e) {
Log.e("Exeption", e.toString());
} catch (Error error) {
Log.e("Error", error.toString());
}
return bmpImage;
}

public static Bitmap combineImages(Bitmap backgroungImage, Bitmap frontImage) { // can
Bitmap cs = null;
try {

cs = Bitmap.createBitmap(backgroungImage.getWidth(), backgroungImage.getHeight(), backgroungImage
.getConfig());

Canvas comboImage = new Canvas(cs);
comboImage.drawBitmap(backgroungImage, 0, 0, null);
comboImage.drawBitmap(frontImage, backgroungImage.getWidth() - frontImage.getWidth(), backgroungImage
.getHeight()
- frontImage.getHeight(), null);

} catch (Exception e) {
Log.e("Error", "Exception = " + e.toString());
} catch (Error e) {
Log.e("Error", "Message = " + e.toString());
}
return cs;
}

}ma​​in.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:background="@color/black"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="wrap_content"
android:paddingRight="2dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:orientation="horizontal"
android:layout_marginRight="4dp">
<ImageView
android:id="@+id/cus_image"
android:src="@drawable/preview_unavailable"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@color/transparent"
android:scaleType="fitCenter" />
<ImageView
android:id="@+id/cus_image4"
android:src="@drawable/preview_unavailable"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@color/black"
android:scaleType="fitCenter" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:paddingRight="2dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:orientation="horizontal"
android:layout_marginRight="4dp">
<WebView
android:id="@+id/web"
android:layout_width="wrap_content"
android:background="@color/black"
android:layout_height="wrap_content" />
</LinearLayout>

最佳答案

你试过吗

Options options = new BitmapFactory.Options(); 
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(activityName.getResources(), R.id.image, options);

代替:

 cs = Bitmap.createBitmap(backgroungImage.getWidth(), backgroungImage.getHeight(), backgroungImage.getConfig()); 

尝试:

cs = Bitmap.createBitmap(backgroungImage.getWidth(), backgroungImage.getHeight(), Bitmap.Config.ARGB_8888);

关于java - 这是解码 InputStream 时 BitmapFactory 的错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5339134/

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