user3102186 user3102186 - 1 month ago 16
Android Question

How to programatically apply effects on imageview image in android?

i want to apply various effects on image view image.this image is already sent from main activity.i have the code for effects but don't know the method how to apply it on button click.kindly guide me.

public class PhotoEffect extends Activity implements OnClickListener {
ImageView camphoto;
Bitmap bmp;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photoeffect);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Bundle extras=getIntent().getExtras();
//b=(Bitmap)extras.get("img");

byte[] byteArray=extras.getByteArray("img");

bmp=BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
camphoto=(ImageView)findViewById(R.id.camimage);
camphoto.setImageBitmap(bmp);


/*if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}*/
}
@Override
public void onClick(View v)
{
Bitmap bitmap = ((BitmapDrawable)camphoto.getDrawable()).getBitmap();
switch (v.getId())
{
case R.id.btnsepia:
//Bitmap bitmap = ((BitmapDrawable)camphoto.getDrawable()).getBitmap();
camphoto.setImageBitmap(createSepiaToningEffect(bitmap, 2, .5, .6, .59));
break;
case R.id.btnhighlight:
camphoto.setImageBitmap(doHighlightImage(bitmap));
break;
}
}

Answer

You must define the createSepiaToningEffect function like this returning a Bitmap after processing.

public static Bitmap createSepiaToningEffect(Bitmap src, int depth, double red, double green, double blue) {
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
    final double GS_RED = 0.3;
    final double GS_GREEN = 0.59;
    final double GS_BLUE = 0.11;
    int A, R, G, B;
    int pixel;

    for(int x = 0; x < width; ++x) {
        for(int y = 0; y < height; ++y) {
            pixel = src.getPixel(x, y);
            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            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; }

            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }

    return bmOut;

}