android - Capture Image while Device is Locked with Password -


i want implement functionality capture image through front camera when tries unlock device , enter incorrect password 3 times. checked possible in android , applications available in market.

i have done work achieve getting black image. here's code :

register device admin broadcast incorrect password attempt :

public class deviceadminsample extends deviceadminreceiver {  static context ctx;  static sharedpreferences getsamplepreferences(context context) {      ctx = context;      return context.getsharedpreferences(             deviceadminreceiver.class.getname(), 0); }  @override public void onpasswordfailed(context context, intent intent) {     super.onpasswordfailed(context, intent);      system.out.println("password attempt failed...");      intent = new intent(context, cameraview.class);     i.setflags(intent.flag_activity_new_task);     context.startactivity(i);  }  } 

camera class capture image , save sd card :

public class cameraview extends activity implements surfaceholder.callback,     onclicklistener { private static final string tag = "cameratest"; camera mcamera; boolean mpreviewrunning = false;  public void oncreate(bundle icicle) {     super.oncreate(icicle);     log.e(tag, "oncreate");      setcontentview(r.layout.cameraview);      msurfaceview = (surfaceview) findviewbyid(r.id.surface_camera);      // msurfaceview.setonclicklistener(this);      msurfaceholder = msurfaceview.getholder();     msurfaceholder.addcallback(this);      msurfaceholder.settype(surfaceholder.surface_type_push_buffers);     msurfaceholder.setkeepscreenon(true);      // msurfaceholder.settype(surfaceholder.surface_type_push_buffers);  }  @override protected void onrestoreinstancestate(bundle savedinstancestate) {     super.onrestoreinstancestate(savedinstancestate); }  protected void onresume() {     log.e(tag, "onresume");     super.onresume(); }  protected void onsaveinstancestate(bundle outstate) {     super.onsaveinstancestate(outstate); }  protected void onstop() {     log.e(tag, "onstop");     super.onstop(); }  public void surfacechanged(surfaceholder holder, int format, int w, int h) {     log.e(tag, "surfacechanged");      // xxx stoppreview() crash if preview not running     if (mpreviewrunning) {         mcamera.stoppreview();     }      camera.parameters p = mcamera.getparameters();      mcamera.setparameters(p);      mcamera.startpreview();     mpreviewrunning = true;     mcamera.takepicture(null, null, mpicturecallback);   }  public void surfacedestroyed(surfaceholder holder) {     log.e(tag, "surfacedestroyed");     // mcamera.stoppreview();     // mpreviewrunning = false;     // mcamera.release();      stopcamera(); }  private surfaceview msurfaceview; private surfaceholder msurfaceholder;  public void onclick(view v) {     mcamera.takepicture(null, mpicturecallback, mpicturecallback); }  public void surfacecreated(surfaceholder holder) {     log.e(tag, "surfacecreated");      int = findfrontfacingcamera();      if (i > 0);      while (true) {         try {             this.mcamera = camera.open(i);             try {                 this.mcamera.setpreviewdisplay(holder);                 return;             } catch (ioexception localioexception2) {                 stopcamera();                 return;             }         } catch (runtimeexception localruntimeexception) {             localruntimeexception.printstacktrace();             if (this.mcamera == null)                 continue;             stopcamera();             this.mcamera = camera.open(i);             try {                 this.mcamera.setpreviewdisplay(holder);                 log.d("hiddeneye plus", "camera open re");                 return;             } catch (ioexception localioexception1) {                 stopcamera();                 localioexception1.printstacktrace();                 return;             }          } catch (exception localexception) {             if (this.mcamera != null)                 stopcamera();             localexception.printstacktrace();             return;         }     } }  private void stopcamera() {     if (this.mcamera != null) {         /*this.mcamera.stoppreview();         this.mcamera.release();         this.mcamera = null;*/         this.mpreviewrunning = false;     } }  private int findfrontfacingcamera() {     int = camera.getnumberofcameras();     (int j = 0;; j++) {         if (j >= i)             return -1;         camera.camerainfo localcamerainfo = new camera.camerainfo();         camera.getcamerainfo(j, localcamerainfo);         if (localcamerainfo.facing == 1)             return j;     } }  camera.picturecallback mpicturecallback = new camera.picturecallback() {      public void onpicturetaken(byte[] data, camera camera) {         if (data != null) {             // intent mintent = new intent();             // mintent.putextra("image",imagedata);              mcamera.stoppreview();             mpreviewrunning = false;             mcamera.release();              try {                 bitmapfactory.options opts = new bitmapfactory.options();                 bitmap bitmap = bitmapfactory.decodebytearray(data, 0,                         data.length, opts);                 bitmap = bitmap.createscaledbitmap(bitmap, 300, 300, false);                 int width = bitmap.getwidth();                 int height = bitmap.getheight();                 int newwidth = 300;                 int newheight = 300;                  // calculate scale - in case = 0.4f                 float scalewidth = ((float) newwidth) / width;                 float scaleheight = ((float) newheight) / height;                  // createa matrix manipulation                 matrix matrix = new matrix();                 // resize bit map                 matrix.postscale(scalewidth, scaleheight);                 // rotate bitmap                 matrix.postrotate(-90);                 bitmap resizedbitmap = bitmap.createbitmap(bitmap, 0, 0,                         width, height, matrix, true);                  bytearrayoutputstream bytes = new bytearrayoutputstream();                 resizedbitmap.compress(bitmap.compressformat.jpeg, 40,                         bytes);                  // can create new file name "test.jpg" in sdcard                 // folder.                 file f = new file(environment.getexternalstoragedirectory()                         + file.separator + "test.jpg");                  system.out.println("file f : " + f );                  f.createnewfile();                 // write bytes in file                 fileoutputstream fo = new fileoutputstream(f);                 fo.write(bytes.tobytearray());                  // remember close de fileoutput                 fo.close();              } catch (exception e) {                 e.printstacktrace();             }             // storebyteimage(mcontext, imagedata, 50,"imagename");             // setresult(foto_mode, mintent);             setresult(585);             finish();         }     } }; } 

i have tested code , proper image think camera code works fine.

you can check code here. copied cameraview class yours. device admin part took https://github.com/marakana/devicepolicydemo picture taken when first attempt fails.

i doubt might kind of hardware issue. not sure if you've seen taking picture camera without preview.

the second answer there mentions faking preview dummy surfaceview not work on android devices. please see blog. he's done test on various phones. try project , if doesn't work on phone. might case. (the other answers might useful you.)

i tried code on galaxy s3 cm10.

hope helps.

edit1: tested on htc one, htc 1 x, sony experia. works fine.

edit2: worked on galaxy note.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -