android - 将滤镜效果应用于 Gallery 中的 Android ImageView

标签 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)

我的效果.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;
    }

最佳答案

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

关于android - 将滤镜效果应用于 Gallery 中的 Android ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20515710/

相关文章:

android - ActionBarSherlock:如何绕过 4.x 设备的 Holo 主题?

java - Android 上的 SMS 消息传递的有趣问题。漏洞?

java - 尝试膨胀布局时适配器出现异常

android - ListView 最后一项之后的分隔符(分隔符)

android - ImageView.ScaleType ="MATRIX": Android 是什么意思

android - 按下 BACK 按钮时 setResult 不起作用

android - AlertDialog 的问题

android - Android ScrollView 中的 VerticalScrollExtent 是什么?

android - 制作静态背景图片

java - 按下按钮时如何更改 ImageView 的图标?