从Gallery中将过滤效果应用于Android ImageViews

时间:2013-12-11 09:46:25

标签: android android-listview android-imageview android-spinner android-image

我正在使用Spinner选择过滤效果列表,如Sapia,GrayScale等等

图像滤镜代码适用于可绘制图像的个别效果。

我想将这些效果应用于那些来自画廊的精选图像。

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

这是OnCreate()

中的所有内容

问题是使用微调器时效果不适用于图像 图像滤镜效果使用http://xjaphx.wordpress.com/2011/06/21/image-processing-photography-sepia-toning-effect/

错误日志:

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

1 个答案:

答案 0 :(得分:1)

photoImage2未初始化,只为您提供NullPointerException

photoImage1初始化两次。

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

所以你可能想要改变这个

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

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