PowenKo > Android > image process > color negative

April 7, 2012   ·   0 Comments

Screen Shot 2012-04-06 at 9.39.51 PM


sample code: Tutorial_UI_imageview_imageprocessActivity

package com.powenko.Tutorial_UI_imageview_imageprocess;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;

public class Tutorial_UI_imageview_imageprocessActivity extends Activity {
    /** Called when the activity is first created. */
    
    private static final int[] FROM_COLOR = new int[]{49, 179, 110};
    private static final int THRESHOLD = 3;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView iv = (ImageView) findViewById(R.id.imageView1);
        Drawable d = getResources().getDrawable(R.drawable.a1);
        iv.setImageDrawable(adjust(d));
    }

    private Drawable adjust(Drawable d)
    {
        int to = Color.RED;

        //Need to copy to ensure that the bitmap is mutable.
        Bitmap src = ((BitmapDrawable) d).getBitmap();
        Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
        for(int x = 0;x < bitmap.getWidth();x++){
            for(int y = 0;y < bitmap.getHeight();y++){
            	/*
                if(match(bitmap.getPixel(x, y))) 
                    bitmap.setPixel(x, y, to);
                    */
            	//bitmap.setPixel(x, y,0xffffffff); //color_gray(bitmap.getPixel(x, y)));
            	bitmap.setPixel(x, y,color_nagative(bitmap.getPixel(x, y)));
            }
        }

        return new BitmapDrawable(bitmap);
    }
    private int color_gray(int pixel)
    {	
    	int t1= (Color.red(pixel)+Color.green(pixel)+Color.green(pixel))/3;			
    	return Color.argb(Color.alpha(pixel), 
    			t1,t1,t1) ; //0xff000000 && t1;
    }
    private int color_nagative(int pixel)
    {	
    	int t1= 255; //(Color.red(pixel)+Color.green(pixel)+Color.green(pixel))/3;			
    	return Color.argb(Color.alpha(pixel), 
    			t1-Color.red(pixel),
    			t1-Color.green(pixel),
    			t1-Color.blue(pixel)
) ; 
    }
    
    
    private boolean match(int pixel)
    {
        //There may be a better way to match, but I wanted to do a comparison ignoring
        //transparency, so I couldn't just do a direct integer compare.
        return Math.abs(Color.red(pixel) - FROM_COLOR[0]) < THRESHOLD &&
            Math.abs(Color.green(pixel) - FROM_COLOR[1]) < THRESHOLD &&
            Math.abs(Color.blue(pixel) - FROM_COLOR[2]) < THRESHOLD;
    }
}
shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Android7

Amazon

adsbygoogle

Recent Posts

Categories

Amazon