gpt4 book ai didi

java - 如何将Uri图片转成Base64?

转载 作者:太空狗 更新时间:2023-10-29 13:55:51 26 4
gpt4 key购买 nike

我已经编写代码将图像从文件位置转换为 base64。我可以轻松地将图像从绝对文件位置转换为 base64,例如:C:/Users/Java Engineer/Desktop/test/gallery/magar/Kanuglam.jpg,但我无法从像
enter image description here.我想从网络服务转换图像以在 android 中使用。这是代码示例:

/**
* TEST JSON
*/
String convertToJsonArrayWithImageForMovieDetailTest(ResultSet rs) {

System.out.println("I am insied json converter");
JSONArray list = new JSONArray();
JSONObject obj ;

//File file;
File locatedFile;
FileInputStream fileInputStream;
try {

while (rs.next()) {
obj = new JSONObject();
System.out.println("inside RS");
System.out.println("date is there ha ha ");

obj.put("movie_name", rs.getString("name"));
obj.put("movie_gener", rs.getString("type"));
String is_free_stuff = rs.getString("is_free_stuff");
if (is_free_stuff == "no") {
is_free_stuff = "PAID";
} else {
is_free_stuff = "FREE";
}
obj.put("movie_type", is_free_stuff);

//String movie_image = rs.getString("preview_image");

//this does not work
String movie_image = "http://www.hamropan.com/stores/slider/2016-09-10-852311027.jpg";

//this works for me
// file = new File("C:/Users/Java Engineer/Desktop/Nike Zoom Basketball.jpg");
locatedFile = new File(movie_image);
// Reading a Image file from file system
fileInputStream = new FileInputStream(locatedFile);
if (locatedFile == null) {
obj.put("movie_image", "NULL");
} else {
byte[] iarray = new byte[(int) locatedFile.length()];
fileInputStream.read(iarray);
byte[] img64 = com.sun.jersey.core.util.Base64
.encode(iarray);
String imageString = new String(img64);
obj.put("movie_image", imageString);
}
list.add(obj);
}


} catch (Exception e) {
e.printStackTrace();
}
return list.toString();
}

这段代码对我有用,但它看起来很慢

public String imageConvertMethod(String url) throws Exception{
ByteArrayOutputStream output = new ByteArrayOutputStream();

try (InputStream input = new URL(url).openStream()) {
byte[] buffer = new byte[512];
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
}

byte [] byte_array = output.toByteArray();

byte[] img64 = com.sun.jersey.core.util.Base64
.encode(byte_array);
String imageString = new String(img64);

return imageString;
}

最佳答案

好的,试试这个

bitmap = getBitmapFromUrl(image_url);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

byte[] array = stream.getByteArray();
encoded_string = Base64.encodeToString(array, 0);

从url加载图片的方法

public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

关于java - 如何将Uri图片转成Base64?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39807480/

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