4

I want to add some effects to images in Android. I am importing the library file in Aviary. I am just a fresher to Android Development. Can anybody be kind enough to provide with a small example on how to use the methods from the library file ? Thanks in advance. Merry Christmas guys. I have also tried an example from here. But I am confused on implementing the example.

CODE :

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.butterfly_image);
        iv.setImageBitmap(src);
        Button b = (Button) findViewById(R.id.button);
        b.setOnClickListener(this);
    }

public void onClick(View arg0) {
    iv = (ImageView) findViewById(R.id.butterfly_image);
    System.out.println("Inside onClick()");
    src = doHighlightImage(src);
    System.out.println("doHighlightImage has been passed");
    iv.setImageBitmap(src);
} 

public static Bitmap doHighlightImage(Bitmap src) {
        // create new bitmap, which will be painted and becomes result image
        Bitmap bmOut = Bitmap.createBitmap(src.getWidth() + 96, src.getHeight() + 96, Bitmap.Config.ARGB_8888);
        // setup canvas for painting
        Canvas canvas = new Canvas(bmOut);
        // setup default color
        canvas.drawColor(0, PorterDuff.Mode.CLEAR);

        // create a blur paint for capturing alpha
        Paint ptBlur = new Paint();
        ptBlur.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
        int[] offsetXY = new int[2];
        // capture alpha into a bitmap
        Bitmap bmAlpha = src.extractAlpha(ptBlur, offsetXY);
        // create a color paint
        Paint ptAlphaColor = new Paint();
        ptAlphaColor.setColor(0xFFFFFFFF);
        // paint color for captured alpha region (bitmap)
        canvas.drawBitmap(bmAlpha, offsetXY[0], offsetXY[1], ptAlphaColor);
        // free memory
        bmAlpha.recycle();

        // paint the image source
        canvas.drawBitmap(src, 0, 0, null);

        // return out final image
        return bmOut;
    }

I am not sure whether the implementation of the above code is right or wrong. It doesn't execute as expected so I guess its wrong. My logcat shows NullPointerException while calling the method.

Logcat :

12-25 16:14:45.150: I/System.out(13695): Inside onClick()
12-25 16:14:45.150: W/dalvikvm(13695): threadid=1: thread exiting with uncaught exception (group=0x4118d438)
12-25 16:14:45.160: E/AndroidRuntime(13695): FATAL EXCEPTION: main
12-25 16:14:45.160: E/AndroidRuntime(13695): java.lang.NullPointerException
12-25 16:14:45.160: E/AndroidRuntime(13695):    at com.example.effects.MainActivity.doHighlightImage(MainActivity.java:47)
12-25 16:14:45.160: E/AndroidRuntime(13695):    at com.example.effects.MainActivity.onClick(MainActivity.java:41)
12-25 16:14:45.160: E/AndroidRuntime(13695):    at android.view.View.performClick(View.java:4101)
12-25 16:14:45.160: E/AndroidRuntime(13695):    at android.view.View$PerformClick.run(View.java:17082)
12-25 16:14:45.160: E/AndroidRuntime(13695):    at android.os.Handler.handleCallback(Handler.java:615)
12-25 16:14:45.160: E/AndroidRuntime(13695):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-25 16:14:45.160: E/AndroidRuntime(13695):    at android.os.Looper.loop(Looper.java:137)
12-25 16:14:45.160: E/AndroidRuntime(13695):    at android.app.ActivityThread.main(ActivityThread.java:4954)
12-25 16:14:45.160: E/AndroidRuntime(13695):    at java.lang.reflect.Method.invokeNative(Native Method)
12-25 16:14:45.160: E/AndroidRuntime(13695):    at java.lang.reflect.Method.invoke(Method.java:511)
12-25 16:14:45.160: E/AndroidRuntime(13695):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
12-25 16:14:45.160: E/AndroidRuntime(13695):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
12-25 16:14:45.160: E/AndroidRuntime(13695):    at dalvik.system.NativeStart.main(Native Method)

I also came to know from certain posts in StackOverFlow that some of the examples from http://xjaphx.wordpress.com are not working as expected. Any help will be appreciated. Merry Christmas.

San
  • 2,078
  • 1
  • 24
  • 42

2 Answers2

2

I guess you src bitmap variable is null. You need to assign the image as below: And you do not need to initialize your ImageView two times as you have already declared it in onCreate()

Change you onClick code as below:

To get the image from your ImageView you need to get the image as below:

Bitmap src= ((BitmapDrawable)iv.getDrawable()).getBitmap();

public void onClick(View arg0) {

     System.out.println("Inside onClick()");
    src = doHighlightImage(src);
    System.out.println("doHighlightImage has been passed");
    iv.setImageBitmap(src);
} 
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • Implementing it now. Will let ya know its result. – San Dec 25 '13 at 10:58
  • It worked man. Thanks pal. I also have another doubt if i may ? – San Dec 25 '13 at 11:01
  • @San Yes, Tell me what is your doubt ? – GrIsHu Dec 25 '13 at 11:36
  • When I try to repeat these effects, I get an OutofMemoryException and I believe this is normal while working with Bitmap. I have surfed and found https://www.youtube.com/watch?v=rsQet4nBVi8 the link. If i add multiple effects in a single activity, will it lead to exception ?? The above mentioned link works only for same sized bitmap images but i want to add these effects to the photo taken by user dynamically. Is it possible to avoid the exception ?? – San Dec 25 '13 at 11:52
  • Well its actually possible but you can not completely ignore this error. While processing with the images you will always face this error. And it can be resolve by resizing the image and reducing its size. – GrIsHu Dec 25 '13 at 11:53
  • Any insights on how to resize and reduce the image size, cause the image is gonna be a pic taken by the user so it will not come in fixed size obviously. Thanks for all your help though. – San Dec 25 '13 at 12:49
  • Check out link http://android-coding.blogspot.in/2011/06/reduce-bitmap-size-using.html also check post http://stackoverflow.com/questions/18573774/how-to-reduce-an-image-file-size-before-uploading-to-a-server this will help you. @San – GrIsHu Dec 25 '13 at 12:51
2

You can see my answer here.It will be useful to handle your bitmap efficiently.

How to make application more responsive which uses several bitmaps?

Community
  • 1
  • 1
Kailash Dabhi
  • 3,473
  • 1
  • 29
  • 47