简体   繁体   中英

How to convert image into Hex String in android?

I am need to convert image into hex string to send it to web server. I am using this method to convert image to byte array

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

and this to convert into hex

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

i want to produce the output as like

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

I am not sure what to do. Do i need to encode it?

The string you have like c11ee236-8f72-4b60-9208-79977d61993f is not an image - it looks more like an ID to an image stored on a server.

If you want the image you have to send the ID to the server and the server sends back the image data stored in it's database that belongs to the ID.

In Java you can generate such random IDs simply by yourself:

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

Outputs for example: 3aa5b32d-c6fb-43c5-80c9-78a1a35aff40

Building your own server you can use this and save both image data and this ID into a database.

you can do this

//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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM