Question

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.

Was it helpful?

Solution

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);
} 

OTHER TIPS

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top