How to convert ARGB to RGB array of bytes? - java

I have a byte array:
byte[] blue_color = {-1,0,112,-64};
How to convert into byte array of RGB?
And also how can I get the real RGB value of the color?

Assuming it's the first element that's the A component:
byte[] rgb = Arrays.copyOfRange(blue_color, 1, 4);
To get the "real" colour values, you need to undo the two's complement representation:
int x = (int)b & 0xFF;

How to convert ARGB array into RGB?
byte[] argb = ...;
byte[] rgb = new byte[(argb.length / 4) * 3];
int index = rgb.length - 1;
for (int i = argb - 1; i >= 0; i -= 4) {
rgb[index--] = argb[i];
rgb[index--] = argb[i - 1];
rgb[index--] = argb[i - 2];
}
How to print integer value:
byte[] oneColor = {..., ..., ..., ...};
int alpha = oneColor[0] & 0xFF;
int red = oneColor[1] & 0xFF;
int green = oneColor[2] & 0xFF;
int blue = oneColor[3] & 0xFF;
System.out.println("Color: " + alpha + ", " + red + ", " + green ", " + blue);
System.out.println("Hexa color: 0x" + Integer.toHexString(alpha) + " " + Integer.toHexString(red) + " " + Integer.toHexString(green) + " " + Integer.toHexString(blue));
Could be done prettier with printf.

How to convert into byte array of RGB?
byte[] rgb = new byte[3];
System.arraycopy(blue_color, 1, rgb, 0, 3);
And also how can I get the real RGB value of the color?
int red = rgb[0] >= 0 ? rgb[0] : rgb[0] + 256;
int green = rgb[1] >= 0 ? rgb[1] : rgb[1] + 256;
int blue = rgb[2] >= 0 ? rgb[2] : rgb[2] + 256;

Related

How read every pixel of image and take couple of last bits from every color channel(RGBA)

So i'm trying to get a byte array of compressed data in an image, the maker told me that I need to read every color channel.
Here's my code so far:
byte[] RGBAb = null;
for (int x = 0; x < bitmap.getWidth(); x++)
{
for (int y = 0; y < bitmap.getHeight(); y++)
{
int mask = 0xFF;
Integer R = Math.abs(bitmap.getPixel(x, y) >> 16 & mask);
Integer G = Math.abs(bitmap.getPixel(x, y) >> 8 & mask);
Integer B = Math.abs(bitmap.getPixel(x, y) & mask);
Integer A = Math.abs(bitmap.getPixel(x, y) >> 24 & mask);
byte Rb = R.byteValue();
byte Gb = G.byteValue();
byte Bb = B.byteValue();
byte Ab = A.byteValue();
RGBAb = new byte[] { Rb, Gb, Bb, Ab};
//System.out.println("Red" + R + "\n Red to bytes: " + Rb);
//System.out.println("Green" + G + "\n Green to bytes: " + Gb);
//System.out.println("Blue" + B + "\n Blue to bytes: " + Bb);
//System.out.println("Alpha" + A + "\n Alpha to bytes: " + Ab);
System.out.println("RGBA Bytes COMBINED \n" + RGBAb);
}
}
I dont know if it's outputting the right value.
This is the photo im reading:
Click Me

How to set custom RGB values while looping through image in Java

I'm looping through an image with X and Y coordinates. However, I can only set an sRGB value using the method: image.setRGB(int x, int y, int rgb);
I want to set a custom RED, GREEN and BLUE values to the current X and Y coordinates. The new RGB values are in this case the average of the original RGB values.
The result I get is a blue image instead of greyscale.
for (int y = 0; y < imageInput.getHeight(); y++) {
for (int x = 0; x < imageInput.getWidth(); x++) {
int clr = imageInput.getRGB(x, y);
Color color = new Color(clr);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int sumRGB = red + green + blue;
int avg = sumRGB/3;
System.out.println("sRGB: " + clr);
System.out.println("Input Image: x= " + x + " y= " + y + " [" + red + "," + green + "," + blue + "]");
System.out.println("Avg RGB: " + avg);
imageCopy.setRGB(x, y, avg);
int clrCopy = imageCopy.getRGB(x, y);
color = new Color(clrCopy);
red = color.getRed();
green = color.getGreen();
blue = color.getBlue();
System.out.println("Output Image: x= " + x + " y= " + y + " [" + red + "," + green + "," + blue + "]\n");
}
}
You could write
Color greyScaleColor = new Color(avg, avg, avg);
int rgbValueForGrey = greyScaleColor.getRGB();
This will give you the int value that you need to set in imageCopy.

Java : Mix two colors in a realistic way

Given two colors A and B, I would like to get the resulting color C, that is the most possible realistic natural mix of the A and B.
Example :
Red + Yellow = Orange
Blue + Yellow = Green
Red + Blue = Purple
Blue + White = Light Blue
Blue + Black = Dark Blue
etc...
Can I get it with ARGB representation of the given colors?
We can call a function which returns result array when give two arrays as parameters. But Arrays should be same sizes.
public int getAvgARGB(int[] clr1, int[] clr2){
int[] returnArray = new int[clr1.length];
for(int i=0; i<clr1.length;i++){
int a1[i] = (clr1[i] & 0xFF000000) >>> 24;
int r1[i] = (clr1[i] & 0x00FF0000) >> 16;
int g1[i] = (clr1[i] & 0x0000FF00) >> 8;
int b1[i] = (clr1[i] & 0x000000FF) ;
int a2[i] = (clr2[i] & 0xFF000000) >>> 24;
int r2[i] = (clr2[i] & 0x00FF0000) >> 16;
int g2[i] = (clr2[i] & 0x0000FF00) >> 8;
int b2[i] = (clr2[i] & 0x000000FF) ;
int aAvg = (a1[i] + a2[i]) / 2;
int rAvg = (r1[i] + r2[i]) / 2;
int gAvg = (g1[i] + g2[i]) / 2;
int bAvg = (b1[i] + b2[i]) / 2;
int returnArray[i] = (aAvg << 24) + (rAvg << 16) + (gAvg << 8) + bAvg;
}
return returnArray;
}

One of RGB value is always 255 - ANDROID

I wrote a simple application that shows you RGB values of touched color from image.
The problem is, everytime i touch my image one of RGB values is 255.
For example. I should have values #F0F0F0 i have #FFF0F0 or #F0FFF0.
Here's my code:
iv = (ImageView) findViewById(R.id.imageView1);
mTextLog = (TextView) findViewById(R.id.textView3);
iv.setOnTouchListener(new OnTouchListener() {
int x = 0, y = 0;
float fx, fy;
public boolean onTouch(View v, MotionEvent event) {
ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
v.getWidth();
v.getHeight();
bitmap.getWidth();
bitmap.getHeight();
fx = ((event.getX()/656)*960);
fy = ((event.getY()/721)*1029);
if(fx > 950) fx = 950;
if(fy > 1000) fy = 1000;
if(fx < 32) fx = 32;
x = Math.round(fx);
y = Math.round(fy);
if(fx > 0 && fy > 0){
int pixel = bitmap.getPixel(x, y);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
if(redValue > 255) redValue = 255;
if(redValue < 0) redValue = 0;
if(greenValue > 255) greenValue = 255;
if(greenValue < 0) greenValue = 0;
if(blueValue > 255) blueValue = 255;
if(blueValue < 0) blueValue = 0;
br = (byte) redValue;
bg = (byte) greenValue;
bb = (byte) blueValue;
tv2.setText("Red: " + redValue + " Green: " + greenValue + " Blue: " + blueValue);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.idd);
rl.setBackgroundColor(pixel);
Another problem is when I moving my finger on screen, chaning color on background is fine, but when i'm trying to send it to my microkontroler via bluetooth there's a problem.
Fe. if i touch black color two times, it sends first black, then blue. :O
It happens only when i return true from this onTouch method.
I would be greatful for any help.
And btw. sorry for my english.
I see some conflicts(Strange!) in your code. first one, if you calculate the colors correctly, so there is no need for checking the values if greater than 255 or less than 0, I mean these lines.
if(redValue > 255) redValue = 255;
if(redValue < 0) redValue = 0;
if(greenValue > 255) greenValue = 255;
if(greenValue < 0) greenValue = 0;
if(blueValue > 255) blueValue = 255;
if(blueValue < 0) blueValue = 0;
and (maybe) image(that working on) color type is different as you expected, as your example mentioned black then blue, maybe this is because the code except the last byte as Alpha or vice versa.
my suggestion is change the image and have another try, or use the following code to ensure which byte is representing the alpha value.
if the image is image/jpeg so the alpha should be 0xFF(255) all the time.
byte[] hcolor;
hcolor=ByteBuffer.allocate(4).putInt(pixel).array();
out(hcolor[0]);//the first byte represent the alpha, so it should be 255.
just have a try and come back and report, we need to fix it.
I changed few lines and the problem still exist.
int pixel = bitmap.getPixel(x, y);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
hcolor = ByteBuffer.allocate(4).putInt(pixel).array();
br = (byte) redValue;
bg = (byte) greenValue;
bb = (byte) blueValue;
//tv2.setText("Red: " + redValue + " Green: " + greenValue >+ " Blue: " + blueValue);
tv2.setText("A: " + hcolor[0] + " R: " + hcolor[1] + " G: " >+ hcolor[2] + " B: " + hcolor[3]);
My textview shows me fe. that: "A: -1 R: -1 G: 25 B: -7" on pink color.
It's impossible to have 2 of 3 RGB values equals -1 if i touched somewhere else than on white.
There's code i use to send color to microcontroler.
"H" is my "key" that uC understands as "Now there'll be a RGB values"
if(mConnectedThread != null){
mConnectedThread.write(h);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
mConnectedThread.write(br);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
mConnectedThread.write(bg);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
mConnectedThread.write(bb);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
When I'm returning "false" from this onTouch method nothing wrong happens.
I just want to touch and move my finger to change color of RGB lamp in real time.
Here's my image i'm using in application.
click

How to sharpen a image in android?

I want to sharpen a image, my code is given below:
public Bitmap RuiHuaBitmap(Bitmap bitmap) {
int width, height;
height = bitmap.getHeight();
width = bitmap.getWidth();
int red, green, blue;
int a1, a2, a3, a4, a5, a6, a7, a8, a9;
Bitmap bmpBlurred = Bitmap.createBitmap(width, height,bitmap.getConfig());
Canvas canvas = new Canvas(bmpBlurred);
canvas.drawBitmap(bitmap, 0, 0, null);
for (int i = 1; i < width - 1; i++) {
for (int j = 1; j < height - 1; j++) {
a1 = bitmap.getPixel(i - 1, j - 1);
a2 = bitmap.getPixel(i - 1, j);
a3 = bitmap.getPixel(i - 1, j + 1);
a4 = bitmap.getPixel(i, j - 1);
a5 = bitmap.getPixel(i, j);
a6 = bitmap.getPixel(i, j + 1);
a7 = bitmap.getPixel(i + 1, j - 1);
a8 = bitmap.getPixel(i + 1, j);
a9 = bitmap.getPixel(i + 1, j + 1);
red = (Color.red(a1) + Color.red(a2) + Color.red(a3) + Color.red(a4) + Color.red(a6) + Color.red(a7) + Color.red(a8) + Color.red(a9)) *(-1) + Color.red(a5)*9 ;
green = (Color.green(a1) + Color.green(a2) + Color.green(a3) + Color.green(a4) + Color.green(a6) + Color.green(a7) + Color.green(a8) + Color.green(a9)) *(-1) + Color.green(a5)*9 ;
blue = (Color.blue(a1) + Color.blue(a2) + Color.blue(a3) + Color.blue(a4) + Color.blue(a6) + Color.blue(a7) + Color.blue(a8) + Color.blue(a9)) *(-1) + Color.blue(a5)*9 ;
bmpBlurred.setPixel(i, j, Color.rgb(red, green, blue));
}
}
return bmpBlurred;
}
but I cannot get the ideal effect. Can someone give more clue, or tell me what is mistake in my code?
Thank you.
You are missing range checking on the rgb values passed to Color.rgb(); you need to normalize rgb values in the [0..255] range before calling Color.rgb() method:
public static int rgb (int red, int green, int blue) Since: API Level
1
Return a color-int from red, green, blue components. The alpha
component is implicity 255 (fully opaque). These component values
should be [0..255], but there is no range check performed, so if they
are out of range, the returned color is undefined. Parameters red Red
component [0..255] of the color green Green component [0..255] of the
color blue Blue component [0..255] of the color
Your convolution matrix seems good for a shapen transformation:
0 0 0 0 0
0 -1 -1 -1 0
0 -1 9 -1 0
0 -1 -1 -1 0
0 0 0 0 0
if you think your effect is too strong, you could also try with:
0 0 0 0 0
0 0 -1 0 0
0 -1 5 -1 0
0 0 -1 0 0
0 0 0 0 0
as an alternative
red = (Color.red(a1) + (Color.red(a2))*(-1) + Color.red(a3) +
(Color.red(a4))*(-1) + (Color.red(a6))*(-1) + Color.red(a7) +
(Color.red(a8))*(-1) + Color.red(a9)) + Color.red(a5)*5 ;
if(red < 0 )
red = 0;
if(red > 255)
red = 255;
green = (Color.green(a1) + (Color.green(a2)) *(-1) + Color.green(a3) +
(Color.green(a4)) *(-1) + (Color.green(a6))*(-1) + Color.green(a7) +
(Color.green(a8))*(-1) + Color.green(a9)) + Color.green(a5)*5 ;
if(green < 0 )
green = 0;
if(green > 255)
green = 255;
blue = (Color.blue(a1) + (Color.blue(a2))*(-1) + Color.blue(a3) +
(Color.blue(a4))*(-1) + (Color.blue(a6))*(-1) + Color.blue(a7) +
(Color.blue(a8)) *(-1) + Color.blue(a9)) + Color.blue(a5)*5 ;
if(blue < 0 )
blue = 0;
if(blue > 255)
blue = 255;

Categories

Resources