java - Android: Camera preview doesn't look nice -
i've written photographic camera app (activity) takes picture, preview shows live image of photographic camera doesn't nice, it's bit tall, :
here code photographic camera preview :
public class camerapreview extends surfaceview implements surfaceholder.callback { private surfaceholder mholder ; private photographic camera mcamera; public camerapreview(context context , photographic camera camera) { super(context) ; mcamera = photographic camera ; mholder = getholder(); mholder.addcallback(this); mholder.settype(surfaceholder.surface_type_push_buffers); } public void surfacecreated(surfaceholder holder) { seek { mcamera.setpreviewdisplay(holder); mcamera.startpreview(); } catch(ioexception e) { log.d(tag,"camera preview failed!: "+e.getmessage()); } } public void surfacechanged(surfaceholder holder , int m , int n , int w) { } public void surfacedestroyed(surfaceholder holder) { } } <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <framelayout android:id="@+id/camera_preview" android:layout_width="wrap_content" android:layout_height="0dip" android:layout_weight="0.86" /> <button android:id="@+id/button_capture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginbottom="16dp" android:gravity="center" android:onclick="capture" android:text="capture" /> <button android:id="@+id/button_accept" android:layout_width="115dp" android:layout_height="wrap_content" android:text="accept" android:visibility="gone" android:onclick = "accept" /> <button android:id="@+id/button_retake" android:layout_width="107dp" android:layout_height="wrap_content" android:text="retake" android:visibility="gone" android:onclick = "retake"/> </linearlayout>
public class iqcameraview extends viewgroup implements surfaceholder.callback { public static final int camera_error = 0; public static final int camera_result = 1; private final string tag = "preview"; private surfaceview msurfaceview; private surfaceholder mholder; private size mpreviewsize; private list<size> msupportedpreviewsizes; private photographic camera mcamera; private boolean mcameraactive; private iqcameracallback mcallback; /** * @param mcallback * of type iqcameracallback * @return of type null setter function mcallback * @since 10 oct 2012 */ public void setmcallback(iqcameracallback mcallback) { this.mcallback = mcallback; } /** * @param context * of type context * @return of type boolean function check scheme has photographic camera * or not * @since 10 oct 2012 */ public static boolean checkcamerahardware(context context) { if (context.getpackagemanager().hassystemfeature( packagemanager.feature_camera)) { homecoming true; } else { homecoming false; } } /** * @param context * constructor function * @since 16 oct 2012 */ public iqcameraview(context context) { super(context); msurfaceview = new surfaceview(context); addview(msurfaceview); mholder = msurfaceview.getholder(); mholder.addcallback(this); mholder.settype(surfaceholder.surface_type_push_buffers); } /** * @param photographic camera * of type photographic camera * @return of type null setter function mcamera * @since 16 oct 2012 */ public void setcamera(camera camera) { mcamera = camera; if (mcamera != null) { msupportedpreviewsizes = mcamera.getparameters() .getsupportedpreviewsizes(); requestlayout(); } } /** * @param of * type null * @return of type null function current frame * photographic camera * @since 16 oct 2012 */ public void takepicture() { if (null != mcamera && mcameraactive) { mcamera.takepicture(null, null, new iqphotohandler(getcontext(), this)); } } /** * @param result * of type string * @return of type null function called when image save * finish * @since 16 oct 2012 */ public void onimagecapture(final string result) { if (null != mcallback) { mcallback.oncameracallback(camera_result, result); } mcamera.startpreview(); } /* * (non-javadoc) * * @see android.view.view#onmeasure(int, int) * * @since 16 oct 2012 */ @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 = getpreviewsize(msupportedpreviewsizes, width, height); } } /* * (non-javadoc) * * @see android.view.viewgroup#onlayout(boolean, int, int, int, int) * * @since 16 oct 2012 */ @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; // 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); } } } /** * @param of * type null * @return of type null function release teh photographic camera * @since 16 oct 2012 */ public void releasecamera() { if (null != mcamera && mcameraactive) { mcamera.stoppreview(); mcamera.release(); mcamera = null; } mcameraactive = false; } /* * (non-javadoc) * * @see * android.view.surfaceholder.callback#surfacecreated(android.view.surfaceholder * ) * * @since 16 oct 2012 */ public void surfacecreated(surfaceholder holder) { seek { if (mcamera != null) { mcamera.setpreviewdisplay(holder); } } grab (ioexception e) { log.e(tag, "ioexception caused setpreviewdisplay()", e); } } /* * (non-javadoc) * * @see android.view.surfaceholder.callback#surfacedestroyed(android.view. * surfaceholder) * * @since 16 oct 2012 */ public void surfacedestroyed(surfaceholder holder) { releasecamera(); } /* * (non-javadoc) * * @see * android.view.surfaceholder.callback#surfacechanged(android.view.surfaceholder * , int, int, int) * * @since 16 oct 2012 */ public void surfacechanged(surfaceholder holder, int format, int w, int h) { camera.parameters parameters = mcamera.getparameters(); parameters.setpreviewsize(mpreviewsize.width, mpreviewsize.height); parameters.setpicturesize(mpreviewsize.width, mpreviewsize.height); parameters.setpictureformat(pixelformat.jpeg); mcamera.setdisplayorientation(90); requestlayout(); seek { mcamera.setparameters(parameters); } grab (exception e) { system.out.print("hih"); } mcamera.startpreview(); mcameraactive = true; } /** * @param sizes * of type list * @param w * of type int * @param h * of type int * @return optionalsize of type size function find exact size * required teh photographic camera view * @since 16 oct 2012 */ private size getpreviewsize(list<size> sizes, int w, int h) { final double aspect_tolerance = 0.1; double targetratio = (double) w / h; if (sizes == null) homecoming null; size optimalsize = null; double mindiff = double.max_value; int targetheight = h; (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); } } } homecoming optimalsize; } /** * @author rajeshcp * @since 16 oct 2012 */ public static interface iqcameracallback { public void oncameracallback(final int type, final object param); } }
try code have used project, hope help
java android camera
No comments:
Post a Comment