java - camera app rotates images by 90 degrees -
my custom camera app developing rotates images 90 degrees time, onscreen view of camera distorted , in 90 degrees anti-clockwise, when saves image, if have taken photo phone 90 degrees left when im holding portrait, takes landscape photos. here code camera activity, can rotate onscreen view of camera?
package fr.altimer.lost.collect.client; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.text.simpledateformat; import java.util.date; import java.util.list; import android.app.activity; import android.content.componentname; import android.content.context; import android.content.intent; import android.content.serviceconnection; import android.graphics.pixelformat; import android.hardware.camera; import android.hardware.camera.size; import android.location.location; import android.os.bundle; import android.os.environment; import android.os.ibinder; import android.view.keyevent; import android.view.surfaceholder; import android.view.surfaceview; import android.view.view; import android.view.view.onclicklistener; import android.view.window; import android.view.windowmanager; import android.widget.imagebutton; import android.widget.toast; import fr.altimer.lost.collect.client.model.data; import fr.altimer.lost.collect.client.model.image; import fr.altimer.lost.collect.client.preferences.configuration; import fr.altimer.lost.collect.client.services.searchservice; import fr.altimer.lost.collect.client.services.servicesensor; public class cameraactivity extends activity implements surfaceholder.callback, camera.picturecallback { private camera camera; private surfaceview surfacecamera; private boolean ispreview; private servicesensor mservice; private boolean mbound; private int routegiven = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // on met l'application en plein écran et sans barre de titre getwindow().setformat(pixelformat.translucent); requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); ispreview = false; // on applique notre layout setcontentview(r.layout.camera); // on récupère notre surface pour le preview surfacecamera = (surfaceview) findviewbyid(r.id.surfaceviewcamera); // bouton pour prendre la photo imagebutton picture = (imagebutton) findviewbyid(r.id.photo); picture.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { takepicture(); } }); // bouton retour imagebutton = (imagebutton) findviewbyid(r.id.tablelayout02); back.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { finish(); } }); // méthode d'initialisation de la caméra initializecamera(); if (savedinstancestate != null) { routegiven = savedinstancestate.getint("routegiven", 0); } } public void initializecamera() { // on attache nos retour du holder à notre activite surfacecamera.getholder().addcallback(this); surfacecamera.getholder().settype(surfaceholder.surface_type_push_buffers); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { // si le mode preview est lancé alors on le stop if (ispreview) { camera.stoppreview(); } // on récupère les parametres de la camera camera.parameters parameters = camera.getparameters(); list<size> sizes = parameters.getsupportedpreviewsizes(); size previewsize = getoptimalpreviewsize(sizes, width, height); // on change la taille parameters.setpreviewsize(previewsize.width, previewsize.height); // on applique nos nouveaux parametres camera.setparameters(parameters); try { // on attache notre previsualisation de la camera au holder de la // surface camera.setpreviewdisplay(surfacecamera.getholder()); } catch (ioexception e) { } // on lance la previeuw camera.startpreview(); ispreview = true; } @override public void surfacecreated(surfaceholder holder) { // on prend le controle de la camera if (camera == null) camera = camera.open(); } @override public void surfacedestroyed(surfaceholder holder) { // on arrête la camera et on rend la main if (camera != null) { camera.stoppreview(); ispreview = false; camera.release(); } } @override public void onpicturetaken(byte[] data, camera camera) { int routeid = 0; // récupération de l'id du parcours if (mbound && mservice != null) { routeid = mservice.getidcurrent(); } else { routeid = routegiven; } // création et sauvegarde la photo string picturename = getfilename(routeid); if (picturename != null) { file picturefile = new file(picturename); fileoutputstream fos = null; try { fos = new fileoutputstream(picturefile); fos.write(data); fos.flush(); toast.maketext(getbasecontext(), "new image saved", toast.length_long).show(); } catch (ioexception e) { e.printstacktrace(); } { try { fos.close(); } catch (ioexception e) { e.printstacktrace(); } } // enregistrement du path de l'image dans la bdd if (mbound && mservice != null) { location loc = mservice.getlastlocation(); data image; if (loc == null) image = new image(picturename, null, null); else image = new image(picturename, loc.getlatitude(), loc.getlongitude()); mservice.savedata(image); } else { intent intent = new intent(); intent.putextra("path", picturename); setresult(result_ok, intent); } } else { toast.maketext(this, "can't create directory save image.", toast.length_long).show(); } finish(); } /** * récupération du chemin de la carte sd * @return */ private file getdir() { string status = environment.getexternalstoragestate(); try { if (status.equals(environment.media_mounted)) { return environment.getexternalstoragedirectory(); } } catch (illegalargumentexception e) { status = environment.media_removed; } return null; } /** * création et récupération du nom du fichier jpg * @param routeid * @return */ private string getfilename(int routeid) { simpledateformat dateformat1 = new simpledateformat("ddmmyy-hhmmss"); string datestring = dateformat1.format(new date()); file filedir = getdir(); if (filedir != null) { file filepath = new file(filedir.getabsolutepath() + configuration.lost_folder); if (!filepath.exists()) filepath.mkdir(); return filepath.getpath() + file.separator + "picture_" + datestring + "_id_" + routeid + ".jpg"; } else { return null; } } private size getoptimalpreviewsize(list<size> sizes, int w, int h) { final double aspect_tolerance = 0.1; double targetratio = (double) w / h; if (sizes == null) return null; size optimalsize = null; double mindiff = double.max_value; int targetheight = h; // try find size match aspect ratio , size (size size : sizes) { double ratio = (double) size.width / size.height; if (math.abs(ratio - targetratio) > aspect_tolerance) continue; if (math.abs(size.height - targetheight) < mindiff) { optimalsize = size; mindiff = math.abs(size.height - targetheight); } } // cannot find 1 match aspect ratio, ignore requirement if (optimalsize == null) { mindiff = double.max_value; (size size : sizes) { if (math.abs(size.height - targetheight) < mindiff) { optimalsize = size; mindiff = math.abs(size.height - targetheight); } } } return optimalsize; } @override public boolean onkeydown(int keycode, keyevent event) { if (keyevent.keycode_camera == keycode /* && event.islongpress() */) { takepicture(); return true; } if (keyevent.keycode_back == keycode) { finish(); return true; } return false; } private void takepicture() { camera.takepicture(null, null, this); } private serviceconnection mconnection = new serviceconnection() { @override public void onserviceconnected(componentname classname, ibinder service) { // we've bound localservice, cast ibinder , localservice instance mservice = ((servicesensor.localbinder) service).getservice(); mservice.sethandler(null); mbound = true; } @override public void onservicedisconnected(componentname arg0) { mbound = false; } }; @override protected void onstart() { super.onstart(); intent intent = new intent(this, servicesensor.class); if (searchservice.search(this, "servicesensor")) { bindservice(intent, mconnection, context.bind_not_foreground); } } @override protected void onstop() { super.onstop(); if (mbound) { unbindservice(mconnection); mbound = false; } } }
(the comments in french working french colleague, although don't speak french much.)
this implementation of surfacecreated
method of surfaceholder.callback
, works fine, please check if missing in implementation
public void surfacecreated(surfaceholder holder) { // surface has been created, acquire camera , tell // draw. try { log.d(tag, "surfacecreated(), holder"+holder.tostring()); mcamera = null; mcamera = camera.open(); log.d(tag, "surfacecreated(), mcamera="+mcamera); mcamera.setdisplayorientation(90); mcamera.setpreviewdisplay(holder); camera.parameters params= mcamera.getparameters(); params.set("jpeg-quality", 72); params.set("rotation", 90); params.set("orientation", "portrait"); params.setpictureformat(pixelformat.jpeg); mcamera.setparameters(params); createzoomlayout(); } catch (exception e) { toast.maketext(camerainterface.this, " surfacecreated " + e.getmessage(), toast.length_long) .show(); e.printstacktrace(); } }
Comments
Post a Comment