Search code examples
androidandroid-listviewandroid-imageviewandroid-spinnerandroid-image

Apply Filter effects to Android ImageViews from Gallery


I'm using Spinner for selecting list of Filter effects Like Sapia,GrayScale...etc

Image Filter code is working for Individual Effects for Drawable images.

I want to apply these effets for selected Images those are piking from Gallery.

MainFrameActivity.java

    filtSp=(Spinner)findViewById(R.id.spnrFilter);
    photoImage1 = (ImageView) findViewById(R.id.ImageV);
    photoImage2 = (ImageView) findViewById(R.id.ImageV1);
    filtSp.setAdapter(new MyFAdapter(MainFrameActivity.this, R.layout.frowview, Filtef));
    filtSp.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View v,
                        int position, long arg3) {

    selectFitem=parent.getItemAtPosition(position).toString();
    if(photoImage1!=null) { 

    if(selectFitem.equals("Sapia" ) {

    bitmap1=BitmapFactory.decodeResource(getResources(), R.id.photoImage);
    bitmap_tmp=EffectsActivity.applySapia(MainActivity.this.bitmap1, 50, 2.2, 0, 2.2);
    photoImage1.setImageBitmap(bitmap_tmp);
                }
    else if(selectFitem.equals("Greyscale" ) {

    bitmap1=BitmapFactory.decodeResource(getResources(), R.id.photoImage);
    bitmap_tmp=EffectsActivity.applyGscale(MainActivity.this.bitmap1);
    photoImage1.setImageBitmap(bitmap_tmp);
                }

            }   
    else if(photoImage2!=null) {    

    if(selectFitem.equals("Sapia" ) {

    bitmap2=BitmapFactory.decodeResource(getResources(), R.id.photoImage2);
    bitmap_tmp=EffectsActivity.applySapia(MainActivity.this.bitmap2, 50, 2.2, 0, 2.2);
    photoImage2.setImageBitmap(bitmap_tmp);
                }
    else if(selectFitem.equals("Greyscale" ) {

    bitmap2=BitmapFactory.decodeResource(getResources(), R.id.photoImage2);
    bitmap_tmp=EffectsActivity.applyGscale(MainActivity.this.bitmap2);
    photoImage2.setImageBitmap(bitmap_tmp);
                }

            }       

                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                }

            });

public class MyFAdapter extends ArrayAdapter<String>{

    public MyFAdapter(Context context, int resource, String[] objects) {
        super(context, resource, objects);
        // TODO Auto-generated constructor stub
            }
    @Override
    public View getDropDownView(int position, View convertView,ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }
    public View getCustomView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater=getLayoutInflater();
        View row=inflater.inflate(R.layout.frowview, parent, false);
        TextView label=(TextView)row.findViewById(R.id.company);
        label.setText(Filtef[position]);


        ImageView icon=(ImageView)row.findViewById(R.id.image);
        icon.setImageResource(arry_Eff[position]);

        return row;
        }
}

this is everything inside of OnCreate()

Problem is Effects are not applying to Images While using Spinner Image Filter Effects are Used from http://xjaphx.wordpress.com/2011/06/21/image-processing-photography-sepia-toning-effect/

Error Log:

12-11 11:00:58.207: E/AndroidRuntime(15784): FATAL EXCEPTION: main
12-11 11:00:58.207: E/AndroidRuntime(15784): java.lang.NullPointerException
12-11 11:00:58.207: E/AndroidRuntime(15784):    at com.Myframes.MyEffects.applySepiaEffect(MyEffects.java:128)
12-11 11:00:58.207: E/AndroidRuntime(15784):    at com.Myframes.MainFrameActivity$1.onItemSelected(MainFrameActivity.java:301)
12-11 11:00:58.207: E/AndroidRuntime(15784):    at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
12-11 11:00:58.207: E/AndroidRuntime(15784):    at android.widget.AdapterView.access$200(AdapterView.java:49)
12-11 11:00:58.207: E/AndroidRuntime(15784):    at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
12-11 11:00:58.207: E/AndroidRuntime(15784):    at android.os.Handler.handleCallback(Handler.java:615)
12-11 11:00:58.207: E/AndroidRuntime(15784):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-11 11:00:58.207: E/AndroidRuntime(15784):    at android.os.Looper.loop(Looper.java:137)
12-11 11:00:58.207: E/AndroidRuntime(15784):    at android.app.ActivityThread.main(ActivityThread.java:4895)
12-11 11:00:58.207: E/AndroidRuntime(15784):    at java.lang.reflect.Method.invokeNative(Native Method)
12-11 11:00:58.207: E/AndroidRuntime(15784):    at java.lang.reflect.Method.invoke(Method.java:511)
12-11 11:00:58.207: E/AndroidRuntime(15784):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
12-11 11:00:58.207: E/AndroidRuntime(15784):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
12-11 11:00:58.207: E/AndroidRuntime(15784):    at dalvik.system.NativeStart.main(Native Method)

MyEffects.java

public class MyEffects {

public static Bitmap applyGscale(Bitmap src) {
        // constant factors
        final double GS_RED = 0.299;
        final double GS_GREEN = 0.587;
        final double GS_BLUE = 0.114;

        // create output bitmap
        Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
        // pixel information
        int A, R, G, B;
        int pixel;

        // get image size
        int width = src.getWidth();
        int height = src.getHeight();

        // scan through every single pixel
        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < height; ++y) {
                // get one pixel color
                pixel = src.getPixel(x, y);
                // retrieve color of all channels
                A = Color.alpha(pixel);
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);
                // take conversion up to one single value
                R = G = B = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
                // set new pixel color to output bitmap
                bmOut.setPixel(x, y, Color.argb(A, R, G, B));
            }
        }

        // return final image
        return bmOut;
    }

public static Bitmap applySapia(Bitmap src, int depth, double red, double green, double blue) {
        // image size
        int width = src.getWidth();
        int height = src.getHeight();
        // create output bitmap
        Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
        // constant grayscale
        final double GS_RED = 0.3;
        final double GS_GREEN = 0.59;
        final double GS_BLUE = 0.11;
        // color information
        int A, R, G, B;
        int pixel;

        // scan through all pixels
        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < height; ++y) {
                // get pixel color
                pixel = src.getPixel(x, y);
                // get color on each channel
                A = Color.alpha(pixel);
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);
                // apply grayscale sample
                B = G = R = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);

                // apply intensity level for sepid-toning on each channel
                R += (depth * red);
                if(R > 255) { R = 255; }

                G += (depth * green);
                if(G > 255) { G = 255; }

                B += (depth * blue);
                if(B > 255) { B = 255; }

                // set new pixel color to output image
                bmOut.setPixel(x, y, Color.argb(A, R, G, B));
            }
        }

        // return final image
        return bmOut;
    }

Solution

  • photoImage2 is not initialized giving you NullPointerException.

    photoImage1 is initialized twice.

    photoImage1 = (ImageView) findViewById(R.id.ImageV);
    photoImage1 = (ImageView) findViewById(R.id.ImageV1);
    

    So you probably want to change this

    photoImage1 = (ImageView) findViewById(R.id.ImageV1);
    

    to

    photoImage2 = (ImageView) findViewById(R.id.ImageV1);