Set the camera focus area in Android -
following several tutorials , examples came next algorithm set camera focus on specific spot, problem camera ignores spot , performs normal overall focus instead of rect area have specified. there else missing in algorithm? has been tested on several phones android 4.0 , above, focus area api supported on these devices. note, app writing works in landscape mode only.
@override public boolean ontouchevent(final motionevent event) { if (event.getaction() == motionevent.action_up) { float x = event.getx(); float y = event.gety(); float touchmajor = event.gettouchmajor(); float touchminor = event.gettouchminor(); rect touchrect = new rect((int)(x - touchmajor / 2), (int)(y - touchminor / 2), (int)(x + touchmajor / 2), (int)(y + touchminor / 2)); this.submitfocusarearect(touchrect); } } private void submitfocusarearect(final rect touchrect) { camera.parameters cameraparameters = camera.getparameters(); if (cameraparameters.getmaxnumfocusareas() == 0) { return; } // convert view's width , height +/- 1000 rect focusarea = new rect(); focusarea.set(touchrect.left * 2000 / camerasurfaceview.getwidth() - 1000, touchrect.top * 2000 / camerasurfaceview.getheight() - 1000, touchrect.right * 2000 / camerasurfaceview.getwidth() - 1000, touchrect.bottom * 2000 / camerasurfaceview.getheight() - 1000); // submit focus area camera arraylist<camera.area> focusareas = new arraylist<camera.area>(); focusareas.add(new camera.area(focusarea, 1000)); cameraparameters.setfocusmode(camera.parameters.focus_mode_auto); cameraparameters.setfocusareas(focusareas); camera.setparameters(cameraparameters); // start autofocus operation camera.autofocus(this); }
before cameraparameters.setfocusareas(focusareas);, should add this:
cameraparameters.setfocusmode(camera.parameters.focus_mode_auto);
Comments
Post a Comment