How can I convert an image into a Base64 string?

前端 未结 14 1180
离开以前
离开以前 2020-11-22 06:52

What is the code to transform an image (maximum of 200 KB) into a Base64 String?

I need to know how to do it with Android, because I have to add the functionali

相关标签:
14条回答
  • 2020-11-22 06:59

    If you need Base64 over JSON, check out Jackson: it has explicit support for binary data read/write as Base64 at both the low level (JsonParser, JsonGenerator) and data-binding level. So you can just have POJOs with byte[] properties, and encoding/decoding is automatically handled.

    And pretty efficiently too, should that matter.

    0 讨论(0)
  • 2020-11-22 06:59

    For those looking for an efficient method to convert an image file to a Base64 string without compression or converting the file to a bitmap first, you can instead encode the file as base64

    val base64EncodedImage = FileInputStream(imageItem.localSrc).use {inputStream - >
        ByteArrayOutputStream().use {outputStream - >
                Base64OutputStream(outputStream, Base64.DEFAULT).use {
                    base64FilterStream - >
                        inputStream.copyTo(base64FilterStream)
                    base64FilterStream.flush()
                    outputStream.toString()
                }
          }
    }
    

    Hope this helps!

    0 讨论(0)
  • 2020-11-22 07:00

    Here is the encoding and decoding code in Kotlin:

     fun encode(imageUri: Uri): String {
        val input = activity.getContentResolver().openInputStream(imageUri)
        val image = BitmapFactory.decodeStream(input , null, null)
    
        // Encode image to base64 string
        val baos = ByteArrayOutputStream()
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos)
        var imageBytes = baos.toByteArray()
        val imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT)
        return imageString
    }
    
    fun decode(imageString: String) {
    
        // Decode base64 string to image
        val imageBytes = Base64.decode(imageString, Base64.DEFAULT)
        val decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
    
        imageview.setImageBitmap(decodedImage)
    }
    
    0 讨论(0)
  • 2020-11-22 07:02
    // Put the image file path into this method
    public static String getFileToByte(String filePath){
        Bitmap bmp = null;
        ByteArrayOutputStream bos = null;
        byte[] bt = null;
        String encodeString = null;
        try{
            bmp = BitmapFactory.decodeFile(filePath);
            bos = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bt = bos.toByteArray();
            encodeString = Base64.encodeToString(bt, Base64.DEFAULT);
        }
        catch (Exception e){
          e.printStackTrace();
        }
        return encodeString;
    }
    
    0 讨论(0)
  • 2020-11-22 07:03

    Instead of using Bitmap, you can also do this through a trivial InputStream. Well, I am not sure, but I think it's a bit efficient.

    InputStream inputStream = new FileInputStream(fileName); // You can get an inputStream using any I/O API
    byte[] bytes;
    byte[] buffer = new byte[8192];
    int bytesRead;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    
    try {
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            output.write(buffer, 0, bytesRead);
        }
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    
    bytes = output.toByteArray();
    String encodedString = Base64.encodeToString(bytes, Base64.DEFAULT);
    
    0 讨论(0)
  • 2020-11-22 07:11

    Below is the pseudocode that may help you:

    public  String getBase64FromFile(String path)
    {
        Bitmap bmp = null;
        ByteArrayOutputStream baos = null;
        byte[] baat = null;
        String encodeString = null;
        try
        {
            bmp = BitmapFactory.decodeFile(path);
            baos = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            baat = baos.toByteArray();
            encodeString = Base64.encodeToString(baat, Base64.DEFAULT);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    
       return encodeString;
    }
    
    0 讨论(0)
提交回复
热议问题