I have the following problem. I started with a bitmap transformation routine that works
flawlessly for any kind of transformation I can throw at it.
Bitmap transform(Bitmap src) {
// ... any kind of transformation , for example GAMMA
double gama = 0.8;
int[] tR = new int[256];
int[] gG = new int[256];
int[] tB = new int[256];
for(int i = 0; i < 256; ++i) {
tR[i] = (int)Math.min(255, (int)((255.0 * Math.pow(i / 255.0, 1.0 / gama)) + 0.5));
tG[i] = (int)Math.min(255, (int)((255.0 * Math.pow(i / 255.0, 1.0 / gama)) + 0.5));
tB[i] = (int)Math.min(255, (int)((255.0 * Math.pow(i / 255.0, 1.0 / gama)) + 0.5));
}
// apply transformation to the old bitmap -> bmOut
int wid = src.getWidth(), hei = src.getHeight();
Bitmap bmOut = Bitmap.createBitmap(wid, hei, src.getConfig());
int A, R, G, B;
for(int x = 0; x < wid; x++) {
for(int y = 0; y < hei; y++) {
int pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = tR[Color.red(pixel)];
G = tG[Color.green(pixel)];
B = tB[Color.blue(pixel)];
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return bmOut;
}
But it is PAINFULLY slow - caused by the getPixel() / setPixel() brothers, sisters.
No problem, says I, I'll just use a memory buffer (like in the old StretchBlt() days). So, I did a MAJOR rewrite, creating the following gem of software engineering :)
Bitmap transform(Bitmap src) {
// ... transformation array are built here
// apply transformation
int wid = src.getWidth(), hei = src.getHeight();
Bitmap bmOut = Bitmap.createBitmap(wid, hei, src.getConfig());
int[] pixs = new int[wid*hei]; // changed
src.getPixels(pixs, 0, wid, 0, 0, wid, hei); // changed
int A, R, G, B;
for(int x = 0; x < wid; x++) {
for(int y = 0; y < hei; y++) {
int off = ( x * y ) + y; // changed
int pixel = pixs[off]; // changed
A = Color.alpha(pixel);
R = tR[Color.red(pixel)];
G = tG[Color.green(pixel)];
B = tB[Color.blue(pixel)];
pixs[off] = Color.argb(A, R, G, B); // changed
}
}
bmOut.setPixels(pixs, 0, wid, 0, 0, wid, hei); // changed
return bmOut;
}
Runs fast, even gets a correct result IF THERE IS NO TRANSFORMATION. But it falls apart
if I try to massage the pixels (apply transformations). So I did comparison of ARGB pixels from getPixel() vs array of pixels values from getPixels(...) and they are different (well, the first 2 are the same, which leaves me with about zillion that are not).
array getPixel
a r g b a r g b
------------------
ff65340b ff65340b
ff64330a ff64330a
ff66320b ff63320a
ff65310a ff613008
ff66300c ff62300d
ff67310d ff62300d
ff68300d ff622d0d
ff69310e ff5f2a0a
....
Anybody knows what I'm doing wrong this time? I am not willing to give up the speed of
the mem-array solution yet.
Thanks, sean
It should be
int off = ( y * wid ) + x;
By the way, I think the two loops is unnecessary, you can simply do:
for (int off = pixs.length - 1; off >= 0; off--)
Related
I am doing an image processing which require to convert RGB bitmap image to YCbCr color space. I retrieved RGB value for each pixel and apply the conversion matrix to it.
public void convertRGB (View v) {
if (imageLoaded) {
int width = inputBM.getWidth();
int height = inputBM.getHeight();
int pixel;
int alpha, red, green, blue;
int Y,Cb,Cr;
outputBM = Bitmap.createBitmap(width, height, inputBM.getConfig());
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
pixel = inputBM.getPixel(x, y);
alpha = Color.alpha(pixel);
red = Color.red(pixel);
green = Color.green(pixel);
blue = Color.blue(pixel);
Y = (int) (0.299 * red + 0.587 * green + 0.114 * blue);
Cb = (int) (128-0.169 * red-0.331 * green + 0.500 * blue);
Cr = (int) (128+0.500 * red - 0.419 * green - 0.081 * blue);
int p = (Y << 24) | (Cb << 16) | (Cr<<8);
outputBM.setPixel(x,y,p);
}
}
comImgView.setImageBitmap(outputBM);
}
}
The problem is he output color is different with original. I tried to use BufferedImage but it do not work in Android
Original:
After Conversion:
May I know what is the correct way to handle YCbCr image in android java.
Try setting using below code
ByteArrayOutputStream out = new ByteArrayOutputStream();
YuvImage yuvImage = new YuvImage(your_yuv_data, ImageFormat.NV21, width, height, null);
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 50, out);
byte[] imageBytes = out.toByteArray();
Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
iv.setImageBitmap(image);
Check documentation for detailed description for YuvImage Class.
I am new to Android and need to process a bitmap to extract the pixel information into a multi-dimensional array by collecting only the black pixels i.e. R = 0, G = 0, and B = 0. I noticed that there is a GetPixel(x, y) method which is apparently slow and I need something like this instead. But I'm lost on how to implement a better GetPixel(x, y) method using information from GetPixels(). I am trying something like this currently:
private static int[][] GetPixels(Bitmap bmp)
{
int height = bmp.getHeight();
int width = bmp.getWidth();
int length = width * height;
int[] pixels = new int[length];
bmp.getPixels(pixels, 0, 0, 0, 0, width, height);
int[][]result = new int[width][height];
for(int pixel = 0, x = 0, y = 0; pixel < pixels.length; pixel += 4)
{
int argb = pixels[pixel];//how to access pixel information?
result[x][y] = argb;//store only black pixels??
x++;
if(y == width)
{
x = 0;
y++;
}
}
return result;
}
I am trying to add snow flow effect. but not succeed
I tried to do the effect of continuously flow the snow.
is it possible? if yes than please give suggestion.
my code is below.
public class MainActivity extends Activity {
private int COLOR_MAX = 0xff;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView)findViewById(R.id.imageView1);
Bitmap imagebitmap = BitmapFactory.decodeResource(getResources(),R.drawable.hydrangeas);
applySnowEffect(imagebitmap);
}
Bitmap applySnowEffect(Bitmap source)
{
// get image size
int width = source.getWidth();
int height = source.getHeight();
int[] pixels = new int[width * height];
// get pixel array from source
source.getPixels(pixels, 0, width, 0, 0, width, height);
// random object
Random random = new Random();
int R, G, B, index = 0, thresHold = 50;
// iteration through pixels
for(int y = 0; y < height; ++y) {
for(int x = 0; x < width; ++x) {
// get current index in 2D-matrix
index = y * width + x;
// get color
R = Color.red(pixels[index]);
G = Color.green(pixels[index]);
B = Color.blue(pixels[index]);
// generate threshold
thresHold = random.nextInt(COLOR_MAX );
if(R > thresHold && G > thresHold && B > thresHold) {
pixels[index] = Color.rgb(COLOR_MAX, COLOR_MAX, COLOR_MAX);
}
}
}
// output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
Toast.makeText(getApplicationContext(),"processed",10).show();
return bmOut;
}
I actually read about this the other day. You can use a particle system for Android. A library that can assist with that is found here - https://github.com/plattysoft/Leonids
Well I've just find the solution.
The source is here: code link
The main idea is SnowFallView extends View, and overriding onDraw(Canvas canvas) event, where we drawing our snow flakes drawables.
The code is just tested and works well.
I am using the following method to change the hue of a bitmap:
public Bitmap changeHue( Bitmap source, double hue ) {
Bitmap result = Bitmap.createBitmap( screenWidth, screenHeight, source.getConfig() );
float[] hsv = new float[3];
for( int x = 0; x < source.getWidth(); x++ ) {
for( int y = 0; y < source.getHeight(); y++ ) {
int c = source.getPixel( x, y );
Color.colorToHSV( c, hsv );
hsv[0] = (float) ((hsv[0] + 360 * hue) % 360);
c = (Color.HSVToColor( hsv ) & 0x00ffffff) | (c & 0xff000000);
result.setPixel( x, y, c );
}
}
return result;
}
It works perfectly and maintains the luminance of the bitmap. However this method is very slow when changing the hue of a bitmap size 800*480 or higher. How can I optimize it without losing too much image quality?
If you wrap your Bitmap in an ImageView there is a very simple way:
ImageView circle = new ImageView(this);
circle.setImageBitmap(yourBitmap);
circle.setColorFilter(Color.RED);
I used the following code to convert a normal image to Gray-scale and Sepia colored image.
for Sepia Color conversion:
public static Bitmap createSepiaToningEffect(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;
}
The above code works fine but the problem is it take more time (more than 60 secs). How can i reduce the time consumption. when i convert the image into Gray-scale it takes less than 2 secs. Can anyone help me to solve this problem.
you can use ndk for it refer to this git repository git://github.com/ruckus/android-image-filter-ndk.git