i'm creating app has camera built myself using camera preview. , works fine on android 4.1
device (the camera opens , takes photos). when try on android 2.3 device, app crashes on framelayout.addview(camera_view)
because when comment line app doesn't crashes. here's code (the measures fit device screen):
private void setupcamera() { camera_view = new camerasurfaceview(getapplicationcontext()); frame_layout = (framelayout) findviewbyid(r.id.image_frame); // v it's square draw v.getlayoutparams().width = (int) ((720/4) + (5*screendensity)); v.getlayoutparams().height = (int) ((720/4) + (5*screendensity)); // img it's imageview display photo img.getlayoutparams().width = (int) (720/4); img.getlayoutparams().height = (int) (720/4); frame_layout.getlayoutparams().width = (int) (720/4); frame_layout.getlayoutparams().height = (int) (960/4); frame_layout.addview(camera_view); }
here camera_view class:
public class camerasurfaceview extends surfaceview implements surfaceholder.callback{ private surfaceholder m_holder; public camera camera = null; public camerasurfaceview(context context) { super(context); m_holder = getholder(); m_holder.addcallback(this); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { camera.parameters params = camera.getparameters(); if (this.getresources().getconfiguration().orientation != configuration.orientation_landscape) { camera.setdisplayorientation(90); } else { camera.setdisplayorientation(0); } params.setflashmode(camera.parameters.flash_mode_auto); params.setfocusmode(camera.parameters.focus_mode_continuous_picture); params.setscenemode(camera.parameters.scene_mode_auto); params.setwhitebalance(camera.parameters.white_balance_auto); params.setexposurecompensation(0); params.setjpegquality(100); list<size> sizes = params.getsupportedpicturesizes(); camera.size size = sizes.get(0); for(int i=0;i<sizes.size();i++) { if(sizes.get(i).width > size.width) size = sizes.get(i); } params.setpreviewsize(size.width, size.height); params.setpicturesize(size.width, size.height); camera.setparameters(params); camera.startpreview(); } @override public void surfacecreated(surfaceholder holder) { camera = camera.open(); try { camera.setpreviewdisplay(m_holder); } catch (ioexception e) { e.printstacktrace(); } } @override public void surfacedestroyed(surfaceholder holder) { camera.stoppreview(); camera.release(); camera = null; } public void capture(camera.picturecallback jpeghandler){ camera.takepicture(null, null, jpeghandler); } }
Comments
Post a Comment