i'm trying use library qrcodereaderview dlazaro66 read qr codes, i'm facing problems, first in phones nexus 5x image shown upside down, solved new method added library, need change selected camera front because default back/rear camera. how can achieve this?
the code of class next, thanks.
public class qrcodereaderview extends surfaceview implements surfaceholder.callback,camera.previewcallback { public interface onqrcodereadlistener { public void onqrcoderead(string text, pointf[] points); public void cameranotfound(); public void qrcodenotfoundoncamimage(); } private onqrcodereadlistener monqrcodereadlistener; private static final string tag = qrcodereaderview.class.getname(); private qrcodereader mqrcodereader; private int mpreviewwidth; private int mpreviewheight; private surfaceholder mholder; private cameramanager mcameramanager; public qrcodereaderview(context context) { super(context); init(); } public qrcodereaderview(context context, attributeset attrs) { super(context, attrs); init(); } public void setonqrcodereadlistener(onqrcodereadlistener onqrcodereadlistener) { monqrcodereadlistener = onqrcodereadlistener; } public cameramanager getcameramanager() { return mcameramanager; } @suppresswarnings("deprecation") private void init() { if (checkcamerahardware(getcontext())){ mcameramanager = new cameramanager(getcontext()); mholder = this.getholder(); mholder.addcallback(this); mholder.settype(surfaceholder.surface_type_push_buffers); // need set flag despite it's deprecated } else { log.e(tag, "error: camera not found"); if (monqrcodereadlistener != null) { monqrcodereadlistener.cameranotfound(); } } } /**************************************************** * surfaceholder.callback,camera.previewcallback ****************************************************/ @override public void surfacecreated(surfaceholder holder) { try { // indicate camera, our view dimensions mcameramanager.opendriver(holder,this.getwidth(),this.getheight()); } catch (ioexception e) { log.w(tag, "can not opendriver: "+e.getmessage()); mcameramanager.closedriver(); } try { mqrcodereader = new qrcodereader(); mcameramanager.startpreview(); } catch (exception e) { log.e(tag, "exception: " + e.getmessage()); mcameramanager.closedriver(); } } @override public void surfacedestroyed(surfaceholder holder) { log.d(tag, "surfacedestroyed"); mcameramanager.getcamera().setpreviewcallback(null); mcameramanager.getcamera().stoppreview(); mcameramanager.getcamera().release(); mcameramanager.closedriver(); } // called when camera take frame @override public void onpreviewframe(byte[] data, camera camera) { planaryuvluminancesource source = mcameramanager.buildluminancesource(data, mpreviewwidth, mpreviewheight); hybridbinarizer hybbin = new hybridbinarizer(source); binarybitmap bitmap = new binarybitmap(hybbin); try { result result = mqrcodereader.decode(bitmap); // notify found qrcode if (monqrcodereadlistener != null) { // transform resultpoints view coordinates pointf[] transformedpoints = transformtoviewcoordinates(result.getresultpoints()); monqrcodereadlistener.onqrcoderead(result.gettext(), transformedpoints); } } catch (checksumexception e) { log.d(tag, "checksumexception"); e.printstacktrace(); } catch (notfoundexception e) { // notify qr not found if (monqrcodereadlistener != null) { monqrcodereadlistener.qrcodenotfoundoncamimage(); } } catch (formatexception e) { log.d(tag, "formatexception"); e.printstacktrace(); } { mqrcodereader.reset(); } } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { log.d(tag, "surfacechanged"); if (mholder.getsurface() == null){ log.e(tag, "error: preview surface not exist"); return; } //preview_width = width; //preview_height = height; mpreviewwidth = mcameramanager.getpreviewsize().x; mpreviewheight = mcameramanager.getpreviewsize().y; mcameramanager.stoppreview(); mcameramanager.getcamera().setpreviewcallback(this); mcameramanager.getcamera().setdisplayorientation(90); // portrait mode // fix camera sensor rotation setcameradisplayorientation(this.getcontext(), mcameramanager.getcamera()); mcameramanager.startpreview(); } /** * transform result surfaceview coordinates * * method needed because coordinates given in landscape camera coordinates. * working transform operations aren't explained * * todo re-write method explaining each single value * * @return new pointf array transformed points */ private pointf[] transformtoviewcoordinates(resultpoint[] resultpoints) { pointf[] transformedpoints = new pointf[resultpoints.length]; int index = 0; if (resultpoints != null){ float previewx = mcameramanager.getpreviewsize().x; float previewy = mcameramanager.getpreviewsize().y; float scalex = this.getwidth()/previewy; float scaley = this.getheight()/previewx; (resultpoint point :resultpoints){ pointf tmppoint = new pointf((previewy- point.gety())*scalex, point.getx()*scaley); transformedpoints[index] = tmppoint; index++; } } return transformedpoints; } /** check if device has camera */ private boolean checkcamerahardware(context context) { if (context.getpackagemanager().hassystemfeature(packagemanager.feature_camera)){ // device has camera return true; } else if (context.getpackagemanager().hassystemfeature(packagemanager.feature_camera_front)){ // device has front camera return true; } else if (context.getpackagemanager().hassystemfeature(packagemanager.feature_camera_any)){ // device has camera return true; } else { // no camera on device return false; } } /** * fix camera sensor no devices (ex.: nexus 5x) * https://developer.android.com/reference/android/hardware/camera.html#setdisplayorientation(int) */ @suppresswarnings("deprecation") public static void setcameradisplayorientation(context context, android.hardware.camera camera) { if (android.os.build.version.sdk_int >= android.os.build.version_codes.gingerbread) { camera.camerainfo info = new camera.camerainfo(); android.hardware.camera.getcamerainfo(0, info); windowmanager windowmanager = (windowmanager) context.getsystemservice(context.window_service); int rotation = windowmanager.getdefaultdisplay().getrotation(); int degrees = 0; switch (rotation) { case surface.rotation_0: degrees = 0; break; case surface.rotation_90: degrees = 90; break; case surface.rotation_180: degrees = 180; break; case surface.rotation_270: degrees = 270; break; } int result; if (info.facing == camera.camerainfo.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); } } }
you must change cameramanager.opencamera() method.
Comments
Post a Comment