Working with Images

All your problems with Audio, Video and Images.

Working with Images

Postby Monu » Wed Jan 14, 2009 7:11 am

I wish to write a simple program using which i can play with images. Like, say, I should be able to load a couple of images(overlap them, so that if transparency of the one on top is increased the other should become visible), work on their pixels, change images properties (like color and alpha), apply filters. After making the changes, i should be able to restore the images to their original state.

I am thinking of using Canvas class and bitmaps. unfortunately, my knowlege is "virtually" zero in this area. Can anyone help me out with this? Any pointers,code snippets or links to tutorials with be great help.

Thanks.
Monu
Freshman
Freshman
 
Posts: 8
Joined: Thu Jan 08, 2009 7:33 am

Top

Postby sooraj.rit » Wed Jan 14, 2009 10:50 am

Hi , please see this code, which can be used to control contrast of an image as well as its transparency/opacity.



package img.control;


import java.io.*;
import java.nio.Buffer;

import org.apache.http.entity.InputStreamEntity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
//import android.view.MotionEvent;
import android.view.View;
import img.control.R;
import android.app.Activity;
import android.content.Context;
import android.view.KeyEvent;

public class ImgControl extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new ImgView(this));
}

private static class ImgView extends View
{
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private ColorMatrix mCM = new ColorMatrix();
private Bitmap mBitmap;
private float mAngle = 0;
private Drawable imgobj;


public ImgView(Context context)
{
super(context);
setFocusable(true);
mBitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.location );
}

private static ColorMatrix setContrast( float contrast )
{
float scale = contrast + 1.f;
float translate = (-.5f * scale + .5f) * 255.f;

ColorMatrix cm = new ColorMatrix();
cm.set(new float[] {
scale, 0, 0, 0, translate,
0, scale, 0, 0, translate,
0, 0, scale, 0, translate,
0, 0, 0, 1, 0 });
return cm;
}

@Override protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
float x = 5;
float y = 5;
canvas.drawColor(Color.WHITE);
mPaint.setColorFilter(null);
canvas.drawBitmap(mBitmap, x, y, mPaint);
invalidate();
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch( keyCode )
{
case KeyEvent.KEYCODE_DPAD_LEFT:
{
SetImgAlpha(true);
break;
}
case KeyEvent.KEYCODE_DPAD_RIGHT:
{
SetImgAlpha(false);
break;
}
case KeyEvent.KEYCODE_DPAD_UP:
{
ProcessContrast( true);
break;
}
case KeyEvent.KEYCODE_DPAD_DOWN:
{
ProcessContrast(false);
break;
}
}
return true;
}

private void SetImgAlpha( boolean bvar )
{
int nAlpha = mPaint.getAlpha();
// Adjust alpha
if( bvar )
{
nAlpha += 5;
if( nAlpha >= 255 )
{
nAlpha = 255;
}
}
else
{
nAlpha -= 5;
if( nAlpha <= 0)
{
nAlpha = 0;
}
}
mPaint.setAlpha(nAlpha);
invalidate();
return;
}

private void ProcessContrast( boolean bvar )
{
if( bvar )
{
mAngle += 10;
if (mAngle > 180)
{
mAngle = 180;
}
}
else
{
mAngle -= 10;
if (mAngle < 0 )
{
mAngle = 0;
}
}
// //convert our animated angle [-180...180] to a contrast value of [-1..1]
float contrast = mAngle / 180.f;

mCM = setContrast( contrast );
mPaint.setColorFilter(new ColorMatrixColorFilter(mCM));
invalidate();
}
}
}
sooraj.rit
Freshman
Freshman
 
Posts: 5
Joined: Tue Jan 13, 2009 12:29 pm
Location: Trivandrum,India

Postby Monu » Wed Jan 14, 2009 1:20 pm

Thanks for your reply..

i copy-pasted your code and ran it....on left and right key presses i could "see" teh alpha adjustments but i couldnot see the response for up and down keys..i wonder why???


Also, can you explain your set contrast function please? As i understand, this matrix will be multiplied with pixels of the bitmap to get new RGB and alpha. Why is the matrix dimension 4X5?
Monu
Freshman
Freshman
 
Posts: 8
Joined: Thu Jan 08, 2009 7:33 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 10 guests