【问题标题】:How to convert image into Hex String in android?如何在android中将图像转换为十六进制字符串?
【发布时间】:2012-05-23 02:27:09
【问题描述】:

我需要将图像转换为十六进制字符串以将其发送到 Web 服务器。我正在使用这种方法将图像转换为字节数组

         BitmapFactory.Options options = new BitmapFactory.Options();
         options.inSampleSize = 8; 
         Bitmap receipt = BitmapFactory.decodeFile(photo.toString(),options);
         int size = receipt.getRowBytes() * receipt.getHeight();  
         ByteArrayOutputStream stream = new ByteArrayOutputStream();
         receipt.compress(Bitmap.CompressFormat.JPEG, 90, stream);
         receiptbyte = stream.toByteArray();   
         String hexstring = toHex(receiptbyte);  

这个转换成十六进制

   public static String toHex(byte[] bytes) {
    BigInteger bi = new BigInteger(1, bytes); 
    return String.format("%0" + (bytes.length << 1) + "X", bi);
}

我想像这样产生输出

c11ee236-8f72-4b60-9208-79977d61993f

我不知道该怎么做。我需要编码吗?

【问题讨论】:

标签: java android hex type-conversion image-conversion


【解决方案1】:

c11ee236-8f72-4b60-9208-79977d61993f 之类的字符串不是图像 - 它看起来更像是存储在服务器上的图像的 ID。

如果您想要图像,您必须将 ID 发送到服务器,然后服务器将存储在属于该 ID 的数据库中的图像数据发回。

在 Java 中,您可以简单地自己生成这样的随机 ID:

UUID u = UUID.randomUUID();
System.out.println(u.toString());

例如输出:3aa5b32d-c6fb-43c5-80c9-78a1a35aff40

构建您自己的服务器,您可以使用它并将图像数据和此 ID 保存到数据库中。

【讨论】:

  • 感谢您的回答...这太棒了,它不是图像的 id 到图像...您知道如何生成该 id 并将其链接到服务器.. 实际上这就是我我正在寻找..请帮我+1这个答案
  • 那很好,但在看到这个答案之前我发现.. 但你的答案指向正确的方向.. 谢谢我标记为答案
【解决方案2】:

你可以这样做

//encode image to base64 string
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] imageBytes = baos.toByteArray();
        String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
 
        //decode base64 string to image
        imageBytes = Base64.decode(imageString, Base64.DEFAULT);
        Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
        image.setImageBitmap(decodedImage);

https://www.thecrazyprogrammer.com/2016/10/android-convert-image-base64-string-base64-string-image.html

【讨论】:

    猜你喜欢
    • 2015-01-15
    • 2013-02-07
    • 2017-08-23
    • 2014-03-19
    • 1970-01-01
    • 2011-08-25
    • 2014-04-28
    相关资源
    最近更新 更多