Reputation: 1
i wrote my image from ImageReader surface and got a very bad photo, is it the ratio ? they are all the same (w & h), is it something with TotalCaptureResult ? , please check the photo and code below, appreciate your help.
1- My original photo from SurfaceView (Screenshotted)
2- my output photo from my ImageReader Surface that i have targetted in my captureRequest.
3- check out my code, all codes in one function and i call this in the OnCreate.
code :
@RequiresApi(Build.VERSION_CODES.R)
private fun camera() {
val thread = HandlerThread("Hthread1").apply {
start()
}
val handler = Handler(thread.looper).apply {
post {
//-------------------------------------------------
val w = 1920
val h = 1080
val Rec = Rect(0,0,w,h)
val imageReader = ImageReader.newInstance(w,h,ImageFormat.YUV_420_888,4)
val cm = getSystemService(Context.CAMERA_SERVICE) as CameraManager
val surfaceview = findViewById<SurfaceView>(R.id.surfaceView).apply {
}
surfaceview.holder.addCallback(object : SurfaceHolder.Callback {
override fun surfaceCreated(p0: SurfaceHolder) {
val surface = surfaceview.holder.surface
val surface2 = imageReader.surface
if (ActivityCompat.checkSelfPermission(
applicationContext,
Manifest.permission.CAMERA
) != PackageManager.PERMISSION_GRANTED
) {
requestPermissions(arrayOf(Manifest.permission.CAMERA), 777777)
return
}
cm.getCameraCharacteristics(cm.cameraIdList.last()).get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES)
cm.openCamera(
cm.cameraIdList.last(),
mainExecutor,
object : CameraDevice.StateCallback() {
override fun onOpened(p0: CameraDevice) {
val cr =
p0.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE)
.apply {
set(CaptureRequest.COLOR_CORRECTION_MODE,CaptureRequest.COLOR_CORRECTION_MODE_HIGH_QUALITY)
addTarget(surface2)
addTarget(surface)
}.build()
p0.createCaptureSession(
SessionConfiguration(SessionConfiguration.SESSION_REGULAR,
mutableListOf(
OutputConfiguration(surface),OutputConfiguration(surface2)
),
mainExecutor,
object : CameraCaptureSession.StateCallback() {
override fun onConfigured(p0: CameraCaptureSession) {
p0.setRepeatingRequest(
cr,
object :
CameraCaptureSession.CaptureCallback() {
},
null
)
}
override fun onConfigureFailed(p0: CameraCaptureSession) {
}
})
)
}
override fun onDisconnected(p0: CameraDevice) {
}
override fun onError(p0: CameraDevice, p1: Int) {
}
})
}
override fun surfaceChanged(p0: SurfaceHolder, p1: Int, p2: Int, p3: Int) {
}
override fun surfaceDestroyed(p0: SurfaceHolder) {
}
})
//-------------------------------------------------
imageReader.setOnImageAvailableListener({
val image = it.acquireLatestImage()
if (image!=null) {
val buffer = image.planes.last().buffer
val byteArray = ByteArray(buffer.remaining())
buffer.get(byteArray)
val YuvImage = YuvImage(byteArray, ImageFormat.YUY2, w, h, null)
findViewById<Button>(R.id.button).setOnClickListener {
val file = FileOutputStream(File.createTempFile("image", ".jpg", filesDir))
YuvImage.compressToJpeg(Rec,85,file)
}
imageReader.discardFreeBuffers()
image.close()
}
},null)
}
}
Edit: Tow photos one from ImageReader and the other a screenshot of the SurfaceView preview.
Upvotes: 0
Views: 187