Photoshop image blending mode in Android -


after lot of searching able create photoshop blending mode filter in android. here working code image blending mode in android working android 2.1 4.4 enjoy , incase of query feel free ask :)

public class mainactivity extends activity {     button btnloadimage1, btnloadimage2;     textview textsource1, textsource2;     button btnprocessing;     imageview imageresult;      final int rqs_image1 = 1;     final int rqs_image2 = 2;     int blendmode=1;     uri source1, source2;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          btnloadimage1 = (button) findviewbyid(r.id.loadimage1);         btnloadimage2 = (button) findviewbyid(r.id.loadimage2);         textsource1 = (textview) findviewbyid(r.id.sourceuri1);         textsource2 = (textview) findviewbyid(r.id.sourceuri2);         btnprocessing = (button) findviewbyid(r.id.processing);         imageresult = (imageview) findviewbyid(r.id.result);         /*         svg svg = svgparser.getsvgfromresource(getresources(), r.raw.businnes);         picture picture = svg.getpicture();         drawable drawable = svg.createpicturedrawable();         imageresult.setimagedrawable(drawable);*/         btnloadimage1.setonclicklistener(new onclicklistener() {              @override             public void onclick(view arg0) {                 intent intent = new intent(                         intent.action_pick,                         android.provider.mediastore.images.media.external_content_uri);                 startactivityforresult(intent, rqs_image1);             }         });          btnloadimage2.setonclicklistener(new onclicklistener() {              @override             public void onclick(view arg0) {                 intent intent = new intent(                         intent.action_pick,                         android.provider.mediastore.images.media.external_content_uri);                 startactivityforresult(intent, rqs_image2);             }         });          btnprocessing.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                  if (source1 != null && source2 != null) {                     bitmap processedbitmap = processingbitmap(blendmode);                     blendmode++;                     if(blendmode>7)                         blendmode=1;                     if (processedbitmap != null) {                         imageresult.setimagebitmap(processedbitmap);                         /*toast.maketext(getapplicationcontext(), "done",                                 toast.length_long).show();*/                     } else {                         toast.maketext(getapplicationcontext(),                                 "something wrong in processing!",                                 toast.length_long).show();                     }                 } else {                     toast.maketext(getapplicationcontext(),                             "select both image!", toast.length_long).show();                 }              }         });     }      @override     protected void onactivityresult(int requestcode, int resultcode, intent data) {         super.onactivityresult(requestcode, resultcode, data);         if (resultcode == result_ok) {             switch (requestcode) {             case rqs_image1:                 source1 = data.getdata();                 textsource1.settext(source1.tostring());                 break;             case rqs_image2:                 source2 = data.getdata();                 textsource2.settext(source2.tostring());                 break;             }         }     }      @suppresslint("newapi")     private bitmap processingbitmap(int value) {         bitmap bm1 = null;         bitmap bm2 = null;         bitmap newbitmap = null;          try {             bm1 = bitmapfactory.decodestream(getcontentresolver()                     .openinputstream(source1));             bm2 = bitmapfactory.decodestream(getcontentresolver()                     .openinputstream(source2));              int w;             if (bm1.getwidth() >= bm2.getwidth()) {                 w = bm1.getwidth();             } else {                 w = bm2.getwidth();             }              int h;             if (bm1.getheight() >= bm2.getheight()) {                 h = bm1.getheight();             } else {                 h = bm2.getheight();             }              config config = bm1.getconfig();             if (config == null) {                 config = bitmap.config.argb_8888;             }                newbitmap = bitmap.createbitmap(w, h, config);             canvas newcanvas = new canvas(newbitmap);              newcanvas.drawbitmap(bm1, 0, 0, null);              paint paint = new paint();             switch (value) {             case key.keys_blend_darken:                 print_toast("blend_darken");                 paint.setxfermode(new porterduffxfermode(mode.darken));                 break;             case key.keys_blend_multiply:                 print_toast("blend_multiply");                 paint.setxfermode(new porterduffxfermode(mode.multiply));                 break;             case key.keys_blend_add:                 print_toast("blend_add");                 paint.setxfermode(new porterduffxfermode(mode.add));                 break;             case key.keys_blend_desolve:                 print_toast("blend_desolve");                 paint.setxfermode(new porterduffxfermode(mode.dst));                 break;              case key.keys_blend_desolve_lighten:                 print_toast("blend_lighten");                 paint.setxfermode(new porterduffxfermode(mode.lighten));                 break;             case key.keys_blend_desolve_overlay:                  print_toast("blend_overlay");                 paint.setxfermode(new porterduffxfermode(mode.overlay));                 break;             case key.keys_blend_desolve_screen:                 print_toast("blend_screen");                 paint.setxfermode(new porterduffxfermode(mode.screen));                 break;               default:                 break;             }              paint.setshader(new bitmapshader(bm2, tilemode.clamp, tilemode.clamp));             /*paint.setalpha(128);             paint.setdither(true);             paint.setantialias(true);*/             //paint.setxfermode(new porterduffxfermode(android.graphics.porterduff.mode.dst_in));             newcanvas.drawrect(0, 0, bm2.getwidth(), bm2.getheight(), paint);             //newcanvas.drawbitmap(bm2, 0, 0, paint);          } catch (filenotfoundexception e) {             // todo auto-generated catch block             e.printstacktrace();         }          return newbitmap;     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }     void printlog(string tag,string value){         log.d(tag, value);     }     void print_toast(string value){         toast.maketext(mainactivity.this, value, toast.length_short).show();     } } 

here key class

public class key {     final public static int keys_blend_darken=1;     final public static int keys_blend_multiply=2;;     public static final int keys_blend_add = 3;     public static final int keys_blend_desolve = 4;     public static final int keys_blend_desolve_lighten = 5;     public static final int keys_blend_desolve_overlay = 6;     public static final int keys_blend_desolve_screen = 7;  } 

xml file activity_main

    <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:tools="http://schemas.android.com/tools"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:paddingbottom="@dimen/activity_vertical_margin"         android:paddingleft="@dimen/activity_horizontal_margin"         android:paddingright="@dimen/activity_horizontal_margin"         android:paddingtop="@dimen/activity_vertical_margin"         tools:context=".mainactivity" >          <button             android:id="@+id/loadimage1"             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:text="load image1" />          <textview             android:id="@+id/sourceuri1"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_alignleft="@+id/loadimage1"             android:layout_below="@+id/loadimage1"             android:layout_marginleft="62dp"             android:text="textview" />          <button             android:id="@+id/loadimage2"             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:layout_alignleft="@+id/loadimage1"             android:layout_below="@+id/sourceuri1"             android:text="load image2" />          <textview             android:id="@+id/sourceuri2"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_alignright="@+id/sourceuri1"             android:layout_below="@+id/loadimage2"             android:text="textview" />          <button             android:id="@+id/processing"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_alignleft="@+id/loadimage2"             android:layout_alignright="@+id/loadimage2"             android:layout_below="@+id/sourceuri2"             android:text="processing" />          <imageview             android:id="@+id/result"             android:layout_width="fill_parent"             android:layout_height="fill_parent"             android:layout_below="@+id/processing"             android:layout_centerhorizontal="true"             android:src="@drawable/ic_launcher" />  </relativelayout> 


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -