android - Taking a picture then check its orientation, then rotate it then save it -


i've tried create application (with unity3d) uses intent take photo , save it.

my issue saving picture checking orientation rotate if needed , saving again, long.

i think that's due fact i'm using tools.

do have tips reduce process time?

here's (edited) code, in advance :

package com.falsename.my;  import java.io.file; import java.io.ioexception; import java.text.simpledateformat; import java.util.date;  import android.app.activity; import android.content.context; import android.content.intent; import android.content.pm.activityinfo; import android.content.pm.packagemanager; import android.net.uri; import android.provider.mediastore; import android.util.log;  public class photoplugin implements myandroidplugin  {     private boolean cantakephotos;     private string mcurrentphotopath;     private static photoplugin instance;     private myactivity activity;     private file photofile;      static final int request_image_capture = 1;      private file createimagefile() throws ioexception      {         // create image file name         string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date());         string imagefilename = "jpeg_" + timestamp + "_";         file storagedir = this.activity.getexternalfilesdir(null);         file image = file.createtempfile(             imagefilename,  /* prefix */             ".jpg",         /* suffix */             storagedir      /* directory */         );          // save file: path use action_view intents         mcurrentphotopath = image.getabsolutepath();         return image;     }       private void dispatchtakepictureintent(myactivity a)      {         if(cantakephotos)         {             intent takepictureintent = new intent(mediastore.action_image_capture);             // ensure there's camera activity handle intent             if (takepictureintent.resolveactivity(a.getpackagemanager()) != null) {                 // create file photo should go                 photofile = null;                 try {                     photofile = createimagefile();                 } catch (ioexception ex) {                     log.e("my", ex.tostring());                 }                 // continue if file created                 if (photofile != null) {                     takepictureintent.putextra(mediastore.extra_output,                             uri.fromfile(photofile));                     takepictureintent.putextra(mediastore.extra_screen_orientation,                              activityinfo.screen_orientation_portrait);                     a.startactivityforresult(takepictureintent, request_image_capture);                 }             }         }     }       public static void takepicture()     {         instance.dispatchtakepictureintent(instance.activity);         }      @override     public void onactivitycreated(context c, myactivity a)      {         instance = this;         this.activity = a;         cantakephotos = a.getpackagemanager().hassystemfeature(packagemanager.feature_camera);             }       @override     public void onactivityresume(myactivity a) {         // todo auto-generated method stub      }      @override     public void onactivitypause(myactivity a) {      }      @override     public void onactivitynewintent(myactivity a, intent intent)      {      }       @override     public void onactivityresult(myactivity a,         int requestcode, int resultcode, intent data)     {                 if (resultcode == activity.result_ok && requestcode == request_image_capture)             {                 a.sendmessage("photolistener", "onphototaken", "");                  photopluginorientationcheckerparams params = new photopluginorientationcheckerparams();                 params.activity = a;                 params.path = this.mcurrentphotopath;                 params.file = this.photofile;                  new photopluginorientationchecker().execute(params);             }     }     } 

and :

package com.falsename.my;  import java.io.file; import java.io.fileoutputstream; import java.io.ioexception;  import android.content.contentresolver; import android.graphics.bitmap; import android.graphics.matrix; import android.media.exifinterface; import android.net.uri; import android.os.asynctask; import android.util.log;  public class photopluginorientationchecker extends asynctask<photopluginorientationcheckerparams, void, void> {         myactivity a;     string path;      file file;      @override     protected void doinbackground(photopluginorientationcheckerparams ... params) {          = params[0].activity;         path = params[0].path;         file = params[0].file;          try          {             exifinterface ei = new exifinterface(path);             int orientation = ei.getattributeint(exifinterface.tag_orientation, exifinterface.orientation_normal);              switch(orientation)              {                 case exifinterface.orientation_rotate_90:                     savebitmap(rotatebitmap(getbitmapfromuri(a, uri.fromfile(file)), 90));                     break;                 case exifinterface.orientation_rotate_180:                     savebitmap(rotatebitmap(getbitmapfromuri(a, uri.fromfile(file)), 180));                     break;                 case exifinterface.orientation_rotate_270:                     savebitmap(rotatebitmap(getbitmapfromuri(a, uri.fromfile(file)), 270));                     break;             }         }         catch(ioexception e)         {             log.e("my", "photo error");         }          return null;     }      @override     protected void onpostexecute(void result)      {         a.sendmessage("photolistener", "onphotosaved", path);         }       private void savebitmap(bitmap bitmap)     {         fileoutputstream out = null;         try         {             out = new fileoutputstream(path);             bitmap.compress(bitmap.compressformat.png, 100, out); // bmp bitmap instance             // png lossless format, compression factor (100) ignored         }          catch (exception e)          {             e.printstacktrace();         }                   {             try             {                 if (out != null)                  {                     out.close();                 }             }              catch (ioexception e)             {                 e.printstacktrace();             }         }     }      public static bitmap rotatebitmap(bitmap source, float angle)     {           matrix matrix = new matrix();           matrix.postrotate(angle);           return bitmap.createbitmap(source, 0, 0, source.getwidth(), source.getheight(), matrix, true);     }      public bitmap getbitmapfromuri(myactivity a, uri imageuri)      {         contentresolver cr = a.getcontentresolver();         cr.notifychange(imageuri, null);         bitmap bitmap;         try {             bitmap = android.provider.mediastore.images.media.getbitmap(cr, imageuri);             return bitmap;         } catch (exception e) {             e.printstacktrace();             return null;         }     } } 

with :

package com.falsename.my;  import java.io.file;  public class photopluginorientationcheckerparams  {     public myactivity activity;     public string path;     public file file; } 

the best solution found :

  1. use background operate images
  2. take image, scale down (i've done .1f ratio), operations, it's quite fast, use result.
  3. do operations on final image, once it's finished, use it.

if final image long compute, scaled 1 not, shows user picture has been taken.

here's checker code (not cleaned now) :

package com.falsename.my;  import java.io.fileoutputstream; import java.io.ioexception;  import android.content.contentresolver; import android.graphics.bitmap; import android.graphics.matrix; //import android.media.exifinterface; import android.net.uri; import android.os.asynctask; import android.util.log;  public class photopluginorientationchecker extends asynctask<photopluginorientationcheckerparams, void, void> {         photopluginorientationcheckerparams params;      @override     protected void doinbackground(photopluginorientationcheckerparams ... params) {          this.params = params[0];          log.v("my", "final mode");          if(this.params.highbitmap == null)         {             this.params.highbitmap = getbitmapfromuri(this.params.activity, uri.fromfile(this.params.file));                     this.params.angle = (this.params.highbitmap.getwidth() > this.params.highbitmap.getheight()) ? 90 : 0;                 }          bitmap bmp = this.params.highbitmap;         string path = this.params.path;          if(this.params.low)         {             path += "_thumb.jpg";             bmp = hightolowbitmap(bmp);         }          if(this.params.angle != 0)         {             log.v("my", "rotate");             savebitmap(rotatebitmap(bmp, this.params.angle), path);         }          return null;     }      private static bitmap hightolowbitmap(bitmap bmp)     {         return bitmap.createscaledbitmap(bmp, bmp.getwidth() / 10, bmp.getheight() / 10, true);     }      @override     protected void onpostexecute(void result)      {         log.v("my", "onpostexecute");                  if(this.params.low)         {             this.params.low = false;             this.params.activity.sendmessage("photolistener", "onthumbnailsaved", this.params.path + "_thumb.jpg");                             new photopluginorientationchecker().execute(params);         }         else         {             this.params.activity.sendmessage("photolistener", "onphotosaved", this.params.path);         }     }       private static void savebitmap(bitmap bitmap, string path)     {         fileoutputstream out = null;         try         {             out = new fileoutputstream(path);             bitmap.compress(bitmap.compressformat.jpeg, 100, out);         }          catch (exception e)          {             e.printstacktrace();         }                   {             try             {                 if (out != null)                  {                     out.close();                 }             }              catch (ioexception e)             {                 e.printstacktrace();             }         }     }      public static bitmap rotatebitmap(bitmap source, float angle)     {           matrix matrix = new matrix();           matrix.postrotate(angle);           return bitmap.createbitmap(source, 0, 0, source.getwidth(), source.getheight(), matrix, true);     }      public bitmap getbitmapfromuri(myactivity a, uri imageuri)      {         contentresolver cr = a.getcontentresolver();         cr.notifychange(imageuri, null);         bitmap bitmap;         try {             bitmap = android.provider.mediastore.images.media.getbitmap(cr, imageuri);             return bitmap;         } catch (exception e) {             e.printstacktrace();             return null;         }     } } 

with :

package com.falsename.my;  import java.io.file;  import android.graphics.bitmap;  public class photopluginorientationcheckerparams  {     public myactivity activity;     public string path;     public file file;     public boolean low = true;     public bitmap highbitmap;     public int angle; } 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -