从Gallery向Android ImageView应用过滤效果
我正在使用微调器来选择过滤效果列表,如Sapia、灰度等 图像过滤器代码用于可绘制图像的单个效果 我想将这些效果应用于从图库中选取的图像 MainFrameActivity.java从Gallery向Android ImageView应用过滤效果,android,android-listview,android-imageview,android-spinner,android-image,Android,Android Listview,Android Imageview,Android Spinner,Android Image,我正在使用微调器来选择过滤效果列表,如Sapia、灰度等 图像过滤器代码用于可绘制图像的单个效果 我想将这些效果应用于从图库中选取的图像 MainFrameActivity.java filtSp=(Spinner)findViewById(R.id.spnrFilter); photoImage1 = (ImageView) findViewById(R.id.ImageV); photoImage2 = (ImageView) findViewById(R.id.Im
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;
}
}
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;
}
公共类MyEffects{
公共静态位图ApplyScale(位图src){
//常数因子
最终双GS_红=0.299;
最终双GS_绿=0.587;
最终双GS_蓝=0.114;
//创建输出位图
位图bmOut=Bitmap.createBitmap(src.getWidth()、src.getHeight()、src.getConfig());
//像素信息
int A,R,G,B;
整数像素;
//获取图像大小
int width=src.getWidth();
int height=src.getHeight();
//扫描每一个像素
对于(int x=0;x255){R=255;}
G+=(深度*绿色);
如果(G>255){G=255;}
B+=(深度*蓝色);
如果(B>255){B=255;}
//设置新像素颜色以输出图像
设置像素(x,y,Color.argb(A,R,G,B));
}
}
//返回最终图像
返回bmOut;
}
photoImage2
未初始化,给您NullPointerException
photoImage1
初始化两次
photoImage1 = (ImageView) findViewById(R.id.ImageV);
photoImage1 = (ImageView) findViewById(R.id.ImageV1);
所以你可能想改变这个
photoImage1 = (ImageView) findViewById(R.id.ImageV1);
到
第128行是什么
MyEffects.java
prospectphotoImage2
为空我编辑了我的Ans问题不在photoImage1/photoImage2这是我在这里键入问题时犯的错误那么第128行是什么呢?@Raghunandan MyEffects.java包含所有图像过滤效果,如Sapia,第128行的灰度方法为空,即“在logcat展示的时候,@KlynCrishnan很高兴我能帮忙。
photoImage1 = (ImageView) findViewById(R.id.ImageV1);
photoImage2 = (ImageView) findViewById(R.id.ImageV1);