Add a dotted circle on android camera interface -
Add a dotted circle on android camera interface -
i'm developing face recognition app.i need display little dotted circle on photographic camera interface place face of person user can train app taking pictures different scales. followed "this link" don't know how on photographic camera interface. can please upload sample project or give me move on project? in advance..
edit-2013-02-14 ok code working. got 2 problems.
i can draw circle. it's not in same position. when screen size changes, it's position changing. tried getwidht() , getheight() methods couldn't draw circle in center of screen.do have thought that?
according reply have create views programatically. need add together capture button interface. can please explain answer?
edit :
code edited draw circle @ center , add together capture image button on surface view.
import java.io.file; import java.io.filenotfoundexception; 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.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.dashpatheffect; import android.graphics.paint; import android.graphics.paint.style; import android.graphics.point; import android.hardware.camera; import android.hardware.camera.picturecallback; import android.os.bundle; import android.os.environment; import android.util.log; import android.view.display; import android.view.surfaceholder; import android.view.surfaceview; import android.view.view; import android.view.viewgroup.layoutparams; import android.view.window; import android.widget.button; import android.widget.framelayout; import android.widget.toast; public class testactivity extends activity { /** called when activity first created. */ photographic camera mcamera = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.camera_layout); mcamera = getcamerainstance(); preview mpreview = new preview(this, mcamera); framelayout preview = (framelayout) findviewbyid(r.id.camera_preview); preview.addview(mpreview); display display = getwindowmanager().getdefaultdisplay(); point size = new point(); display.getsize(size); int screencenterx = (size.x /2); int screencentery = (size.y/2) ; drawontop mdraw = new drawontop(this,screencenterx,screencentery); addcontentview(mdraw, new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content)); //adding listener button capturebutton = (button) findviewbyid(r.id.button_capture); capturebutton.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { mcamera.takepicture(null, null, mpicture); } }); } /** * helper method access photographic camera returns null if * cannot photographic camera or not exist * @return */ private photographic camera getcamerainstance() { photographic camera camera = null; seek { photographic camera = camera.open(); } grab (exception e) { // cannot photographic camera or not exist } homecoming camera; } picturecallback mpicture = new picturecallback() { @override public void onpicturetaken(byte[] data, photographic camera camera) { file picturefile = getoutputmediafile(); if (picturefile == null){ return; } seek { fileoutputstream fos = new fileoutputstream(picturefile); fos.write(data); fos.close(); toast.maketext(testactivity.this, "photo saved folder \"pictures\\mycameraapp\"", toast.length_short).show(); } grab (filenotfoundexception e) { } grab (ioexception e) { } } }; private static file getoutputmediafile(){ file mediastoragedir = new file(environment.getexternalstoragepublicdirectory( environment.directory_pictures), "mycameraapp"); if (! mediastoragedir.exists()){ if (! mediastoragedir.mkdirs()){ log.d("mycameraapp", "failed create directory"); homecoming null; } } // create media file name string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date()); file mediafile; mediafile = new file(mediastoragedir.getpath() + file.separator + "img_"+ timestamp + ".jpg"); homecoming mediafile; } } class drawontop extends view { int screencenterx = 0; int screencentery = 0; final int radius = 50; public drawontop(context context, int screencenterx, int screencentery) { super(context); this.screencenterx = screencenterx; this.screencentery = screencentery; } @override protected void ondraw(canvas canvas) { // todo auto-generated method stub paint p = new paint(); p.setcolor(color.red); dashpatheffect dashpath = new dashpatheffect(new float[]{5,5}, (float)1.0); p.setpatheffect(dashpath); p.setstyle(style.stroke); canvas.drawcircle(screencenterx, screencentery, radius, p); invalidate(); super.ondraw(canvas); } } //---------------------------------------------------------------------- class preview extends surfaceview implements surfaceholder.callback { surfaceholder mholder; photographic camera mcamera; preview(context context, photographic camera camera) { super(context); // install surfaceholder.callback notified when this.mcamera = camera; // underlying surface created , destroyed. mholder = getholder(); mholder.addcallback(this); //this deprecated method, not required after 3.0 mholder.settype(surfaceholder.surface_type_push_buffers); } public void surfacecreated(surfaceholder holder) { // surface has been created, acquire photographic camera , tell // draw. seek { mcamera.setpreviewdisplay(holder); mcamera.startpreview(); } grab (ioexception e) { // todo auto-generated grab block e.printstacktrace(); } } public void surfacedestroyed(surfaceholder holder) { // surface destroyed when return, stop // because cameradevice object not shared resource, // of import release when activity paused. mcamera.stoppreview(); mcamera.release(); mcamera = null; } public void surfacechanged(surfaceholder holder, int format, int w, int h) { // size known, set photographic camera parameters // preview. camera.parameters parameters = mcamera.getparameters(); list<camera.size> previewsizes = parameters.getsupportedpreviewsizes(); // need take appropriate previewsize app camera.size previewsize = previewsizes.get(0); parameters.setpreviewsize(previewsize.width, previewsize.height); mcamera.setparameters(parameters); mcamera.startpreview(); } }
camera_layout.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <framelayout android:id="@+id/camera_preview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" /> <button android:id="@+id/button_capture" android:text="capture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </linearlayout>
don't forget add together permission:
<uses-permission android:name="android.permission.camera" /> <uses-feature android:name="android.hardware.camera" />
new permission added:
<uses-permission android:name="android.permission.write_external_storage" />
android android-camera android-canvas
Comments
Post a Comment