android - Auto Focus Modes Not Working In Samsung S8 -
update 2: focus modes apparently don't work
update 1: problem seems isolated samsung s8 device. it's working on nexus 5x , xiaomi phones
i've been trying make focus_mode_continuous_picture
work no avail. i've confirmed device supports continuous auto picture via getsupportedfocusmodes()
, , not sure why doesn't work.
here's code, continuous auto focus being set in startcamera()
:
class capturereceiptfragment : fragment(), capturereceiptcontract.view { private val my_permissions_request_camera = 1 private var camera: camera? = null private var hascamerapermission: boolean? = null private lateinit var preview: camerapreview override var presenter: capturereceiptcontract.presenter? = null private val picture = camera.picturecallback { data, camera -> val picturestoragedir = file(environment.getexternalstoragepublicdirectory( environment.directory_pictures), getappname()) presenter?.savereceipt(picturestoragedir, data) } override fun oncreateview(inflater: layoutinflater?, container: viewgroup?, savedinstancestate: bundle?): view? { val root = inflater!!.inflate(r.layout.capturereceipt_frag, container, false) return root } override fun onactivitycreated(savedinstancestate: bundle?) { super.onactivitycreated(savedinstancestate) hascamerapermission = contextcompat.checkselfpermission(context, manifest.permission.camera) != packagemanager.permission_granted if (hascamerapermission!!) { showcameraerrormsg() } else { capturereceipt_button_capturereceipt.setonclicklistener { capturereceipt() } startcameraoncreate() } } override fun onresume() { super.onresume() presenter?.start() if (hascamerapermission!!) { showcameraerrormsg() } else { startcamera() setcamerarotation() } } override fun onpause() { super.onpause() camera?.release() camera = null } override fun gotoreceiptlist() { //todo } override fun goback() { //todo } private fun startcameraoncreate() { preview = camerapreview(context) capturereceipt_framelayout_viewfinder.addview(preview) startcamera() } private fun startcamera() { if (camera == null) { camera = camera.open() if (camera == null) { showcameraerrormsg() } else { val params: camera.parameters? = camera!!.parameters if (params!!.getsupportedfocusmodes() .contains(camera.parameters.focus_mode_continuous_picture)) { params.focusmode = camera.parameters.focus_mode_continuous_picture; } camera?.parameters = params preview.camera = camera capturereceipt_textview_cameraerrormsg.visibility = view.gone capturereceipt_framelayout_viewfinder.visibility = view.visible } } } private fun showcameraerrormsg() { capturereceipt_textview_cameraerrormsg.visibility = view.visible capturereceipt_framelayout_viewfinder.visibility = view.gone } override fun capturereceipt() { camera?.takepicture(null, null, picture) } private fun getoutputpicturefile(): file? { val appname = getappname() val picturestoragedir = file(environment.getexternalstoragepublicdirectory( environment.directory_pictures), appname) if (!picturestoragedir.exists()) { picturestoragedir.mkdirs() } val timestamp = simpledateformat("yyyymmdd_hhmmss").format(date()) return file(picturestoragedir.getpath() + file.separator + "img_" + timestamp + ".jpg") } private fun getappname(): string { val appinfo = context.getapplicationinfo() val stringid = appinfo.labelres if (stringid == 0) { return appinfo.nonlocalizedlabel.tostring() } else { return this.getstring(stringid) } } fun setcameradisplayorientation() { val info = camera.camerainfo() camera.getcamerainfo(camera_facing_back, info) val rotation = activity.windowmanager.defaultdisplay.rotation var degrees = 0 when (rotation) { surface.rotation_0 -> degrees = 0 surface.rotation_90 -> degrees = 90 surface.rotation_180 -> degrees = 180 surface.rotation_270 -> degrees = 270 } var result: int if (info.facing == camera_facing_front) { result = (info.orientation + degrees) % 360 result = (360 - result) % 360 // compensate mirror } else { // back-facing result = (info.orientation - degrees + 360) % 360 } camera?.setdisplayorientation(result); } fun setcamerarotation() { val info: camera.camerainfo = camera.camerainfo() camera.getcamerainfo(camera_facing_back, info) val rotation = activity.windowmanager.defaultdisplay.rotation var degrees = 0 when(rotation) { surface.rotation_0 -> degrees = 0 surface.rotation_90 -> degrees = 90 surface.rotation_180 -> degrees = 180 surface.rotation_270 -> degrees = 270 } val rotate = (info.orientation - degrees + 360) % 360 camera?.setdisplayorientation(rotate) val params: camera.parameters = camera!!.parameters params.setrotation(rotate) camera?.parameters = params } companion object { @jvmstatic fun newinstance(): capturereceiptfragment { return capturereceiptfragment() } }
}
any appreciated. thanks!
i figured out -- appears autofocus mode parameters should set differently in samsung s8.
here's tried failed:
- setting autofocus modes (auto, continuous_picture, infinity) before starting preview
- scheduled
camera.autofocus()
run every 1 second immdediately after camera.open
here's tried worked:
- putting continuous focus or
camera.autofocus()
in button onclick - delaying setting of continuous focus 1 second after
camera.open()
<-- solved issue
my code #2:
private val autofocusexecutor = scheduledthreadpoolexecutor(1) fun startcamera() { if (camera == null) { camera = camera.open() if (camera == null) { showcameraerrormsg() } else { preview.camera = camera capturereceipt_textview_cameraerrormsg.visibility = view.gone capturereceipt_framelayout_viewfinder.visibility = view.visible autofocusexecutor.schedule(sc{ val params: camera.parameters = camera!!.parameters if (params.getsupportedfocusmodes() .contains(camera.parameters.focus_mode_continuous_picture)) { //todo: auto focus not working params.focusmode = camera.parameters.focus_mode_continuous_picture } camera?.parameters = params }, 1000, timeunit.milliseconds) } } }
Comments
Post a Comment