Unable to take image using front camera: Android -
i unable take pictures using front camera. test device definately has both front , camera/functionality. reason pictures being taken camera. here have. note: camera , parameter globally set.
@override public void surfacecreated(surfaceholder holder) { camera = camera.open(); parameter = camera.getparameters(); camera.size size = getbestpreviewsize(surfaceview.getwidth(), surfaceview.getheight(), parameter); parameter.setpreviewsize(size.width, size.height); try { camera.setpreviewdisplay(surfaceholder); } catch (throwable ignored) { log.e(tag, "set preview error: ", ignored); } camera.setparameters(parameter); } }
in surfacechanged()
start camera preview.
@override public void surfacechanged(surfaceholder holder, int format, int width, int height) { // todo auto-generated method stub if(ispreview == true) { camera.stoppreview(); ispreview = false; } if (camera != null) { try { camera.setpreviewdisplay(surfaceholder); camera.setdisplayorientation(90); camera.startpreview(); ispreview = true; } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
in surfacedestroyed()
release camera resources
@override public void surfacedestroyed(surfaceholder holder) { if (camera != null) { camera.stoppreview(); camera.release(); camera = null; ispreview = false; } }
here referenced method returns preview size
private camera.size getbestpreviewsize(int width, int height, camera.parameters parameters) { camera.size result = null; (camera.size size : parameters.getsupportedpreviewsizes()) { if (size.width <= width && size.height <= height) { if (result == null) { result = size; } else { int resultarea = result.width * result.height; int newarea = size.width * size.height; if (newarea > resultarea) { result = size; } } } } this.size = result; return (result); }
whenever want use front camera--the front camera additional feature, camera used--i call frontcamera()
method. method set camera-id 2, believe flag frontcamera()
. set parameters updated parameters , take picture.
private void frontcamera() { parameter = camera.getparameters(); cameraid = findfrontfacingcamera(); if (cameraid < 0) { //no front face camera } else { parameter.set("camera-id", 2); camera.setparameters(parameter); } camera.takepicture(null, null, mypicturecallback_jpg); }
my check front face camera done in findfrontfacingcamera()
method
private int findfrontfacingcamera() { cameraid = -1; //search front facing camera int numberofcameras = camera.getnumberofcameras(); (int = 0; i<numberofcameras; i++) { camerainfo cinfo = new camerainfo(); camera.getcamerainfo(i, cinfo); if (cinfo.facing == camerainfo.camera_facing_front) { log.d(tag, "front camera found"); cameraid = i; break; } } return cameraid; }
final notes: numberofcameras
returns 2, indicating test device has 2 cameras. also, reaching "front camera found" tag further convinces me not hardware issue. whatever reason, code take images camera. ideas? in advance.
don't use .open()
. use .open(cameraid)
, pass camera id of front facing camera.
Comments
Post a Comment