I want to capture camera imageProxy and save as jpg file with something drawn. My application capture from imageAnalyzer in thread and then execute drawText and compress bitmap to JPG File in other thread. MyApplication based on below repository
https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/android
Some bitmaps don’t draw text. This isn’t occur consistently the portion of drawn and not drawn is changing.
@setting imageAnalyzer
imageAnalyzer =
ImageAnalysis.Builder()
.setTargetAspectRatio(AspectRatio.RATIO_4_3)
.setTargetRotation(fragmentCameraBinding.viewFinder.display.rotation)
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.setOutputImageFormat(OUTPUT_IMAGE_FORMAT_RGBA_8888)
.build()
// The analyzer can then be assigned to the instance
.also {
it.setAnalyzer(cameraExecutor) { image ->
if (!::bitmapBuffer.isInitialized) {
// The image rotation and RGB image buffer are initialized only once
// the analyzer has started running
bitmapBuffer = Bitmap.createBitmap(
image.width,
image.height,
Bitmap.Config.ARGB_8888
)
cameraBitmap = Bitmap.createBitmap(
image.width,
image.height,
Bitmap.Config.ARGB_8888
)
//Width 640 Height 480 * 4 = 1228800
}
Log.d("Image analyzer.Width:",image.width.toString()) //640
Log.d("Image analyzer.Height:",image.height.toString()) // 480
detectObjects(image)
}
}
@detect object using tfmodel
private fun detectObjects(image: ImageProxy) {
// Copy out RGB bits to the shared bitmap buffer
image.use {
bitmapBuffer.copyPixelsFromBuffer(image.planes[0].buffer)
image.planes[0].buffer.rewind()
cameraBitmap.copyPixelsFromBuffer(image.planes[0].buffer)}
val imageRotation = image.imageInfo.rotationDegrees
// Pass Bitmap and rotation to the object detector helper for processing and detection
objectDetectorHelper.detect(bitmapBuffer, imageRotation)
udpExecutors.execute{
sendDataThroughUDP(cameraBitmap)
}
}
@Draw Text
private fun sendDataThroughUDP(cameraBitmap : Bitmap) {
val canvas = Canvas(cameraBitmap)
canvas.drawText("Rotation",cameraBitmap.width / 2.0f,cameraBitmap.height / 2.0f, paint)
if(counter < 10){
Log.d("ImageCapture","{$counter}th capture")
saveImg("rotationTest", cameraBitmap)
}
}
@save jpg file
private fun saveImg(fileName : String, bm : Bitmap){
counter += 1
Log.d("External Storage Path:",Environment.getExternalStorageDirectory().path)
val file = File(Environment.getExternalStorageDirectory().path + "/Pictures/CameraX-Image", "$fileName$counter.jpg")
val fos = FileOutputStream(file)
bm.compress(Bitmap.CompressFormat.JPEG, 100, fos)
fos.close()
}