Friday, April 15, 2011

Auto change color of Image in Android

XML File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView android:id="@+id/picview" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:src="@drawable/pic1" />
    <Button android:id="@+id/changecolor" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dip" android:text="Change" />
</LinearLayout>






Java File

package com.ketan.example;

import java.io.FileOutputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class ChangeColorOfImage extends Activity
{
    private ImageView mIV;
    private Bitmap mBitmap;
    private int picw = 320;
    private int pich = 240;
    private Button mButton;

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

        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);
        picw = mBitmap.getWidth();
        pich = mBitmap.getHeight();
        mIV = (ImageView) findViewById(R.id.picview);
        mButton = (Button)findViewById(R.id.changecolor);
        mIV.setImageBitmap(mBitmap);
        mIV.invalidate();
        mButton.setOnClickListener(mChangeColorListener);
    }

    private OnClickListener mChangeColorListener = new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            TintThePicture(20);
            SaveThePicture();
        }
    };
   
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
        {
            return (true);
        }
        return super.onKeyDown(keyCode, event);
    }

    private void TintThePicture(int deg)
    {
        int[] pix = new int[picw * pich];
        mBitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);

        int RY, BY, RYY, GYY, BYY, R, G, B, Y;
        double angle = (3.14159d * (double) deg) / 180.0d;
        int S = (int) (256.0d * Math.sin(angle));
        int C = (int) (256.0d * Math.cos(angle));

        for (int y = 0; y < pich; y++)
        {
            for (int x = 0; x < picw; x++)
            {
                int index = y * picw + x;
                int r = (pix[index] >> 16) & 0xff;
                int g = (pix[index] >> 8) & 0xff;
                int b = pix[index] & 0xff;
                RY = (70 * r - 59 * g - 11 * b) / 100;
                BY = (-30 * r - 59 * g + 89 * b) / 100;
                Y = (30 * r + 59 * g + 11 * b) / 100;
                RYY = (S * BY + C * RY) / 256;
                BYY = (C * BY - S * RY) / 256;
                GYY = (-51 * RYY - 19 * BYY) / 100;
                R = Y + RYY;
                R = (R < 0) ? 0 : ((R > 255) ? 255 : R);
                G = Y + GYY;
                G = (G < 0) ? 0 : ((G > 255) ? 255 : G);
                B = Y + BYY;
                B = (B < 0) ? 0 : ((B > 255) ? 255 : B);
                pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
            }
        }
       
        Bitmap bm = Bitmap.createBitmap(picw, pich, Bitmap.Config.ARGB_8888);
        bm.setPixels(pix, 0, picw, 0, 0, picw, pich);
        mIV.setImageBitmap(bm);
        mIV.invalidate();
        mBitmap = bm;
        pix = null;
    }

    private void SaveThePicture()
    {
        try
        {
            FileOutputStream fos = super.openFileOutput("output.jpg",
                    MODE_WORLD_READABLE);

            mBitmap.compress(CompressFormat.JPEG, 75, fos);
            fos.flush();
            fos.close();
        }
        catch (Exception e)
        {
            Log.e("TAG", e.toString());
        }
    }
}

1 comment:

  1. Hi,
    I'm new to Android Development. I have got three errors while trying this in Eclipse as
    1. No resource found that matches the given name (at 'src' with value '@drawable/pic1'). main.xml
    2. pic1 cannot be resolved or is not a field ColorChangeActivity.java /ColorChange/src/com/deep/color line 30 Java Problem
    3. id cannot be resolved or is not a field ColorChangeActivity.java /ColorChange/src/com/deep/color line 33 Java Problem

    May u plz help?

    ReplyDelete