问题
I tried to capture camera preview in android device and encode imagefiles to Base64 String. I've already tested like this
// Convert Failed to Image
private fun encoder1(filePath: File): String{
val bytes = filePath.readBytes()
val base64 = android.util.Base64.encodeToString(bytes, android.util.Base64.NO_WRAP)
return base64
}
// Saved Broken Image
private fun encoder2(imageUri: Uri): String {
val input = this.contentResolver.openInputStream(imageUri)
//val bm = BitmapFactory.decodeResource(resources, R.drawable.test)
val image = BitmapFactory.decodeStream(input, null, null)
//encode image to base64 string
val baos = ByteArrayOutputStream()
//bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
image!!.compress(Bitmap.CompressFormat.JPEG, 100, baos)
var imageBytes = baos.toByteArray()
return android.util.Base64.encodeToString(imageBytes, android.util.Base64.NO_WRAP)
//return Base64.getEncoder().encodeToString(imageBytes) // Not Worked, too.
}
// Saved Broken Image
private fun encoder3(path: String): String {
val imagefile = File(path)
var fis: FileInputStream? = null
try {
fis = FileInputStream(imagefile)
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
val bm = BitmapFactory.decodeStream(fis)
val baos = ByteArrayOutputStream()
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos)
val b = baos.toByteArray()
return Base64.getEncoder().encodeToString(b)
}
These encoder called like this
override fun onImageSaved(output: ImageCapture.OutputFileResults) {
var photoPath = photoFile.canonicalPath
val savedUri = Uri.fromFile(photoFile)
Log.e("Before Base64 encoder photoFile", photoFile.toString())
Log.d("Before Base64 encoder", savedUri.toString())
Log.d("Base64 encoder111", encoder1(photoFile))
Log.d("Base64 encoder222", encoder2(savedUri))
Log.d("Base64 encoder333", encoder3(photoPath))
val msg = "Photo capture succeeded: $savedUri"
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
Log.d(TAG, msg)
}
and one of the base64 log/ is...
2020-09-01 15:14:16.485 5295-5295/com.soyeon.cameraxtutorial D/Base64 encoder222: /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE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gtEIvyGYlOU87JxK3c8Hd6WoepbtrwKlOafKCSFMcxMwrPKIY9xnjPNUGvVeNuTehn8JtleEIWV6brh0pfL5e6l80G+ER3nJa1CZ55SZwYNW+DY5jbvlcc3Fvvbq3VC4TASVGEhS3ASENTSMu2r1jR2Ju44w14Ncr7rc2yvoGue9vNMJzduW+S3hGZJbNpK1/wBqBcTJ1F5nwhM+2oJnRM33bKtWtL2WzxCGHCe78G7K11rDD9wiKItEXmA1P6+Mru6PxSEN3jHkzgnkmYPHk7eRU4E8mCHEyKGBivIhA2pZk4Q5ThlaNpx7LZLfCTmTWZzqSqMNs4OocIs6gSmPFrRDi4zDY+uPVYWrG70F9GU4Y3VjmHkrnz8ktfBcNxEeBIZrhJ4BMGGXDv0A+zbOI9lrGulJhSCQz6RC87yneZxPtI3mbSkGWma1sOe28mhjHtdasYWiA9ncW8/NNu0InoGbn6qFkVxLq3VnTqfSSWElrojEstTpPCkrS946lsyeVc5By4Z7eWhg0b5rdWoY2OKCXEG7mGzfBdaYNMdQevvYh+7W2VzCPoDiFQz8zSiUhEODbbYxN7a3Zq3ftssch3AKCTJEwJ5hCEpZhA+/KE3Hn/tUNfttuJRNWL9tWenu+qxJck3QDfa9TG6EpJNoDNOCOnTdm1by1QnJJk8M/CzMxD6k7atmPmuxhvcbFTEgpgIcYIKNmreUuoJptzCDtjCDW5MK+OreuTYRu5cgEvLOCv3pr1DmUxK8YivAyE4by7ej
I printed Base64 String and ctrl+c/ ctrl+v in https://codebeautify.org/base64-to-image-converter for check the results. But i only get broken images like [enter image description here][1] [1]: https://i.stack.imgur.com/eob8v.jpg
i think all the permission has included.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.any" />
saved images has no problems. HELP ME PLEASE!!! https://github.com/paksuua/CameraXtutorial.git
回答1:
You should add the header like this [data:image/png;base64,]
You can try the image covert to base64 online
https://www.base64-image.de/
Check the base64 image data format
回答2:
If you log the base64 string to logcat then only the first bytes of the string are printed.
So copy/paste will only give you part of the base64 string.
来源:https://stackoverflow.com/questions/63682414/imagefilepath-uri-to-base64-encoded-string-has-problem-in-kotlin