Saturday 15 May 2010

java - Custom camera shows dark picture preview when photo taken on Nexus -



java - Custom camera shows dark picture preview when photo taken on Nexus -

i have implemented custom photographic camera , works on other smartphones when take photo in nexus smartphones image stored preview of image shown user on surface view dark not photo stored.

my classes below

preview.java

package com.custom.customcamera; import java.io.ioexception; import java.util.list; import android.content.context; import android.hardware.camera; import android.hardware.camera.parameters; import android.hardware.camera.size; import android.os.handler; import android.util.log; import android.view.surfaceholder; import android.view.surfaceview; import android.view.view; import android.view.viewgroup; class preview extends viewgroup implements surfaceholder.callback { private final string tag = "preview"; surfaceview msurfaceview; surfaceholder mholder; int heightmax ; int widthmax ; size mpreviewsize; list<size> msupportedpreviewsizes; photographic camera mcamera; @suppresswarnings("deprecation") preview(context context, surfaceview sv) { super(context); msurfaceview = sv; // addview(msurfaceview); mholder = msurfaceview.getholder(); mholder.addcallback(this); mholder.settype(surfaceholder.surface_type_push_buffers); } public void setcamera(camera camera) { mcamera = camera; if (mcamera != null) { msupportedpreviewsizes = mcamera.getparameters().getsupportedpicturesizes(); requestlayout(); // photographic camera parameters camera.parameters params = mcamera.getparameters(); list<string> focusmodes = params.getsupportedfocusmodes(); if (focusmodes.contains(camera.parameters.focus_mode_auto)) { // set focus mode params.setfocusmode(camera.parameters.focus_mode_auto); // set photographic camera parameters mcamera.setparameters(params); } params.setwhitebalance(parameters.white_balance_auto); params.setscenemode(parameters.scene_mode_auto); //params.setpreviewformat(256); int index = params.getexposurecompensation (); params.setexposurecompensation(index); mcamera.setparameters(params); } } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { // purposely disregard kid measurements because deed // wrapper surfaceview centers photographic camera preview instead // of stretching it. final int width = resolvesize(getsuggestedminimumwidth(), widthmeasurespec); final int height = resolvesize(getsuggestedminimumheight(), heightmeasurespec); setmeasureddimension(width, height); if (msupportedpreviewsizes != null) { mpreviewsize=maxsize(); } } public size maxsize(){ // heightmax =0; // widthmax =0; size sizemax=msupportedpreviewsizes.get(0); //long totalsize = heightmax*widthmax; //long maxsize=msupportedpreviewsizes.get(0).height*msupportedpreviewsizes.get(0).width; for(size size:msupportedpreviewsizes){ if(size.height*size.width>sizemax.width*sizemax.height){ sizemax = size; } } homecoming sizemax; } @override protected void onlayout(boolean changed, int l, int t, int r, int b) { if (changed && getchildcount() > 0) { final view kid = getchildat(0); final int width = r - l; final int height = b - t; int previewwidth = width; int previewheight = height; if (mpreviewsize != null) { previewwidth = mpreviewsize.width; previewheight = mpreviewsize.height; } // center kid surfaceview within parent. if (width * previewheight > height * previewwidth) { final int scaledchildwidth = previewwidth * height / previewheight; child.layout((width - scaledchildwidth) / 2, 0, (width + scaledchildwidth) / 2, height); } else { final int scaledchildheight = previewheight * width / previewwidth; child.layout(0, (height - scaledchildheight) / 2, width, (height + scaledchildheight) / 2); } } } public void surfacecreated(final surfaceholder holder) { // surface has been created, acquire photographic camera , tell // draw. seek { if (mcamera != null) { mcamera.setpreviewdisplay(holder); } } grab (exception exception) { log.e(tag, "ioexception caused setpreviewdisplay()", exception); } } public void surfacedestroyed(surfaceholder holder) { // surface destroyed when return, stop preview. if (mcamera != null) { mcamera.release(); //mcamera.stoppreview(); mcamera = null; } } camera.autofocuscallback mnautofocuscallback = new camera.autofocuscallback() { @override public void onautofocus(boolean success, photographic camera camera) { } }; public void surfacechanged(surfaceholder holder, int format, int w, int h) { if(mcamera != null) { camera.parameters parameters = mcamera.getparameters(); parameters.setpicturesize(mpreviewsize.width, mpreviewsize.height); requestlayout(); mcamera.setparameters(parameters); mcamera.startpreview(); } } }

cameraactivity.java

package com.custom.customcamera; import java.io.bytearrayoutputstream; import java.io.file; import java.io.fileoutputstream; import java.util.calendar; import java.util.date; import java.util.uuid; import android.util.base64; import com.kut.kutcamera.r; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.matrix; import android.hardware.camera; import android.hardware.camera.camerainfo; import android.hardware.camera.errorcallback; import android.hardware.camera.parameters; import android.hardware.camera.picturecallback; import android.hardware.camera.shuttercallback; import android.media.exifinterface; import android.os.build; import android.os.bundle; import android.os.handler; import android.app.activity; import android.content.intent; import android.util.log; import android.view.motionevent; import android.view.surface; import android.view.surfaceview; import android.view.view; import android.view.view.onclicklistener; import android.widget.framelayout; import android.widget.imageview; public class cameraactivity extends activity { activity context; preview preview; photographic camera camera; imageview fotobutton; //imageview foto; imageview cancelbutton; imageview confirmbutton; //surfaceview surfaceview; string path = ""; static cameralistener listener; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_camera); context=this; intent intent = getintent(); string temppath = intent.getstringextra("path"); this.path = temppath; fotobutton = (imageview) findviewbyid(r.id.imageview_foto); confirmbutton = (imageview) findviewbyid(r.id.imageview_confirm); cancelbutton = (imageview) findviewbyid(r.id.imageview_cancel); //foto = (imageview) findviewbyid(r.id.imageview_phototaken); //surfaceview=(surfaceview)findviewbyid(r.id.customcamerafragment); confirmbutton.setvisibility(view.invisible); cancelbutton.setvisibility(view.invisible); //foto.setvisibility(view.invisible); preview = new preview(this, (surfaceview) findviewbyid(r.id.customcamerafragment)); framelayout frame = (framelayout) findviewbyid(r.id.preview); frame.addview(preview); preview.setkeepscreenon(true); fotobutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { seek { fotobutton.setimageresource(r.drawable.camera_focused); takefocusedpicture(); } grab (exception e) { } fotobutton.setclickable(false); } }); } public void initializelistener(cameralistener listener) { this.listener = listener; } private int cameraid = 0; @override protected void onresume() { super.onresume(); // todo auto-generated method stub seek { if(camera==null){ cameraid = findfrontfacingcamera(); photographic camera = camera.open(cameraid); camera.parameters params = camera.getparameters(); params.setwhitebalance(parameters.white_balance_auto); params.setscenemode(parameters.scene_mode_auto); //params.setpreviewformat(256); int index = params.getexposurecompensation (); params.setexposurecompensation(index); camera.setparameters(params); camera.startpreview(); camera.seterrorcallback(new errorcallback() { public void onerror(int error, photographic camera mcamera) { camera.release(); photographic camera = camera.open(); log.d("camera died", "error camera"); } }); } if (camera != null) { if (build.version.sdk_int >= 14) setcameradisplayorientation(context, camerainfo.camera_facing_front, camera); preview.setcamera(camera); } } catch(exception ex) { string s = ex.tostring(); } } private int findfrontfacingcamera() { int cameraid = -1; // search front end facing photographic camera int numberofcameras = camera.getnumberofcameras(); (int = 0; < numberofcameras; i++) { camerainfo info = new camerainfo(); camera.getcamerainfo(i, info); if (info.facing == camerainfo.camera_facing_front) { cameraid = i; break; } } homecoming cameraid; } private void setcameradisplayorientation(activity activity, int cameraid, android.hardware.camera camera) { android.hardware.camera.camerainfo info = new android.hardware.camera.camerainfo(); android.hardware.camera.getcamerainfo(cameraid, info); int rotation = activity.getwindowmanager().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); } camera.autofocuscallback mautofocuscallback = new camera.autofocuscallback() { @override public void onautofocus(boolean success, final photographic camera camera) { try{ camera.parameters params = camera.getparameters(); //params.setwhitebalance(parameters.white_balance_auto); //params.setscenemode(parameters.scene_mode_auto); //params.setpreviewformat(256); //int index = params.getexposurecompensation (); //params.setexposurecompensation(index); //camera.setparameters(params); //camera.autofocus(null); camera.takepicture(mshuttercallback, null, jpegcallback); }catch(exception e){ string err = e.getmessage(); } } }; camera.shuttercallback mshuttercallback = new shuttercallback() { @override public void onshutter() { // todo auto-generated method stub } }; public void takefocusedpicture() { camera.autofocus(mautofocuscallback); } picturecallback rawcallback = new picturecallback() { public void onpicturetaken(byte[] data, photographic camera camera) { } }; string imagepath = ""; picturecallback jpegcallback = new picturecallback() { public void onpicturetaken(final byte[] data, final photographic camera camerasec) { camera.stoppreview(); //bitmap image = bitmapfactory.decodebytearray(data, 0, data.length); fotobutton.setvisibility(view.invisible); confirmbutton.setvisibility(view.visible); cancelbutton.setvisibility(view.visible); //surfaceview.setvisibility(surfaceview.gone); //foto.setvisibility(view.visible); //foto.setvisibility(imageview.visible); //foto.setimagebitmap(image); confirmbutton.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view paramview, motionevent parammotionevent) { seek { confirmbutton.setimageresource(r.drawable.confirmfocused); fileoutputstream outstream = null; calendar c = calendar.getinstance(); file videodirectory = new file(path); if (!videodirectory.exists()) { videodirectory.mkdirs(); } imagepath = path + "/" + uuid.randomuuid().tostring() + "_" +new date().gettime() + ".jpg"; outstream = new fileoutputstream(imagepath); outstream.write(data); outstream.close(); bitmap realimage; exifinterface exif = new exifinterface(imagepath); int rotation = (int)exiforientationtodegrees( exif.getattributeint(exifinterface.tag_orientation, exifinterface.orientation_normal)); bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 2; realimage = bitmapfactory.decodefile(imagepath, options); bytearrayoutputstream baos = new bytearrayoutputstream(); realimage.compress(bitmap.compressformat.jpeg, 100, baos); //bm bitmap object byte[] bytearrayimage = baos.tobytearray(); string encodedimage = base64.encodetostring(bytearrayimage, base64.default); if(listener != null) listener.picturetaken(encodedimage,imagepath,imagepath.substring(imagepath.lastindexof("/")+1)); intent launchintent = getpackagemanager().getlaunchintentforpackage(getbasecontext().getpackagename()); launchintent.setflags(intent.flag_activity_reorder_to_front); startactivity(launchintent); finish(); } catch(exception ex){} homecoming false; } }); cancelbutton.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view paramview, motionevent parammotionevent) { //surfaceview.setvisibility(surfaceview.visible); cancelbutton.setimageresource(r.drawable.cancelfocused); confirmbutton.setvisibility(view.invisible); cancelbutton.setvisibility(view.invisible); //foto.setvisibility(view.invisible); cancelbutton.setimageresource(r.drawable.cancel); confirmbutton.setimageresource(r.drawable.confirm); fotobutton.setvisibility(view.visible); fotobutton.setclickable(true); fotobutton.setimageresource(r.drawable.camera); camerasec.startpreview(); homecoming false; } }); } }; private float exiforientationtodegrees(int exiforientation) { if (exiforientation == exifinterface.orientation_rotate_90) { homecoming 90; } else if (exiforientation == exifinterface.orientation_rotate_180) { homecoming 180; } else if (exiforientation == exifinterface.orientation_rotate_270) { homecoming 270; } homecoming 0; } public static bitmap rotate(bitmap source, float angle) { matrix matrix = new matrix(); matrix.postrotate(angle); homecoming bitmap.createbitmap(source, 0, 0, source.getwidth(), source.getheight(), matrix, false); } }

i have got work around issue made image brighter , shown in imageview. code used given below:

foto.setcolorfilter(brightit(100));//foto imageview //and below brightit func public static colormatrixcolorfilter brightit(int fb) { colormatrix cmb = new colormatrix(); cmb.set(new float[] { 1, 0, 0, 0, fb, 0, 1, 0, 0, fb, 0, 0, 1, 0, fb, 0, 0, 0, 1, 0 }); colormatrix colormatrix = new colormatrix(); colormatrix.set(cmb); //canvas c = new canvas(b2); //paint paint = new paint(); colormatrixcolorfilter f = new colormatrixcolorfilter(colormatrix); //paint.setcolorfilter(f); homecoming f; }

java android android-camera

No comments:

Post a Comment