how to create a CMYK image from a RGB BITMAP in android - android

just like the title of the question, i want to ask how to create a CMYK image when i have a RGB image as a bitmap.
I had reads question that generate the CMYK value from RGB value just like this code
public static int[] rgbToCmyk(int red, int green, int blue)
{
int black = Math.min(Math.min(255 - red, 255 - green), 255 - blue);
if (black!=255) {
int cyan = (255-red-black)/(255-black);
int magenta = (255-green-black)/(255-black);
int yellow = (255-blue-black)/(255-black);
return new int[] {cyan,magenta,yellow,black};
} else {
int cyan = 255 - red;
int magenta = 255 - green;
int yellow = 255 - blue;
return new int[] {cyan,magenta,yellow,black};
}
}
but, after i have the CMYK value i still don't understand where to place the value just like Bitmap.setBitmap() on android.
as i know the setBitmap function use the RGB value, not CMYK value...
there are a way to change the image color type to CMYK in android?
I'm a newbie in android, correct me if I wrong..

The best thing is a library call to convert the format. Try android-lib-magick, described in the Stack Overflow question Android CMYK mode.

Related

How to change image backarrow color depending on the layout background image color?

Bitmap bitmap = ((BitmapDrawable)itemUserProfileBinding.ivItemUserProfile.getDrawable()).getBitmap();
// Bitmap imageProfile = BitmapFactory.decodeResource(activity.getResources(),arrayList.get(position).getProfile());
int pixel = bitmap.getPixel(20,20);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
int invertedRed = 255 - redValue;
int invertedGreen = 255 - greenValue;
int invertedBlue = 255 - blueValue;
itemUserProfileBinding.ivUserProfileBackArrow.setColorFilter(Color.argb(255, invertedRed, invertedGreen, invertedBlue));
In above code itemUserProfileBinding.ivItemUserProfile is my background imageView and itemUserProfileBinding.ivUserProfileBackArrow this is my custom toolbar back arrow imageView.
I have tried above but it is not working, can any one please help me.
Thanks in advance
I didn't properly understand your question but if you want to invert the complete bitmap then get each pixel in the image and then invert its RGB values.
If itemUserProfileBinding.ivUserProfileBackArrow is your imageview then I think you should first get the bitmap from the imageview and then invert the colours of that bitmap and then set the inverted bitmap to your imageview.
You can use Palette class to generate color and change it accordingly
//Gradle dependency
dependencies {
compile 'com.android.support:palette-v7:2x.x.x'
}
// Java
Palette.from(bitmap).generate(new PaletteAsyncListener() {
public void onGenerated(Palette p) {
// Use generated instance
// this is an Async call, You will get following colors in respect to bitmap
/*Vibrant
Vibrant Dark
Vibrant Light
Muted
Muted Dark
Muted Light*/
itemUserProfileBinding.ivUserProfileBackArrow.setColorFilter(p.getMuted(0x000000));
}
});

Display an image in the form of an int array [] in an ImageView

How do I display an image in int [] format, for example:
int[] UM = {1,1,0,0,1,1,0,0,0,1,1,1,0,0,1,1,1,0,0,1,1,1,0,0,1,1,1,0,0,1,1,1,0,0,1,1,1,0,0,1,1,1,0,0,1};
here is my code:
public class MainActivity extends Activity {
private ImageView imageTeste;
private Bitmap bitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int[] UM = {1,1,0,0,1,1,0,0,0,1,1,1,0,0,1,1,1,0,0,1,1,1,0,0,1,1,1,0,0,1,1,1,0,0,1,1,1,0,0,1,1,1,0,0,1};
bitmap = Bitmap.createBitmap(UM, 5, 9, Bitmap.Config.RGB_565);
imageTeste = (ImageView) findViewById(R.id.imageId);
imageTeste.setImageBitmap(bitmap);
}
}
But it does not display anything, only if I change the Bitmap.Config.ARGB_8888 parameter to RGB_565, but doing it, the white pixels (1) turn black. Even converting values (1) to 255 does not work, they are displayed as blue.
Does anyone know how to solve this problem?
The method you're using is: Bitmap createBitmap (int[] colors, int width, int height, Bitmap.Config config). Take a look at the documentation for this method here. The int[] you supply the method must be an array of Color ints. These are not regular integers and using a 0 or 1 for these values is what is making your image appear empty.
Check out the Android docs on Color here.
For example, opaque blue would be 0xff0000ff. Color ints have 4 components packed in a single 32 bit integer value. The 4 components represent Alpha, Red, Green, and Blue respectively. In the example the A component is FF meaning color is fully opaque. The R component 00, there's no red. The G component is 00, there's no green. The B component is FF, all blue.
You'll just need to give the createBitmap() method a valid int[] and you should solve your problem.

Android Convert a bitmap to Grayscale but keep one Color

I want to set my whole bitmap to grayscale, but I want to keep one color, red or blue or green or any color the user likes?
Is this possible with a colormatrix?
Here's a link, that's what I want to do but in android.
How can I convert an RGB image to grayscale but keep one color?
Thank you very much.
Try this:
ImageView imageView;
Bitmap photo,photo1;
photo1 = Bitmap.createBitmap(photo.getWidth(),photo.getHeight(),photo.getConfig());
for(int i=0;i<photo.getWidth();i++)
{
for(int j=0;j<photo.getHeight();j++){
int p=photo.getPixel(i,j);
int r= Color.red(p);
int g=Color.green(p);
int b=Color.blue(p);
int alpha=Color.alpha(p);
r=0;
g=g+150;
b=0;
alpha=0;
photo1.setPixel(i,j, Color.argb(Color.alpha(p),r,g,b));
}
}
imageView.setImageBitmap(photo1)

Color detection in a static image - OpenCV Android

I have an image with four squares red, green, blue and yellow. I need to get the rgb values of each squares. I'm able to get the rgb of the whole image, but i want it for a specific section.
The image which i am going to get will be from the camera and stored onto the SDCard
I don't know if I understand you exactly, but here it comes.
You need to create BufferedImage object to get RGB value:
File f = new File(yourFilePath);
BufferedImage img = ImageIO.read(f);
You can get RGB Color values from the image from then. You have 4 squares; to check their RGB values, you can check the corner pixels' RGB values:
Color leftTop = new Color(img.getRGB(0, 0));
Color rightTop = new Color(img.getRGB(img.getWidth - 1, 0));
Color leftBottom = new Color(img.getRGB(0, img.getHeight - 1));
Color rightBottom = new Color(img.getRGB(img.getWidth - 1, img.getHeight - 1));
After that it's easy to get red, green and blue values individually:
int red = leftTop.getRed();
int green = leftTop.getGreen();
int blue = leftTop.getBlue();
EDIT:
I'm really sorry, I didn't see it's for Android. As you said, Android doesn't have ImageIO class. To accomplish the task in Android, first initialize the image:
Bitmap img = BitmapFactory.decodeFile(yourFilePath);
From then the operation is pretty much the same:
int leftTop = img.getPixel(0, 0);
...
int red = Color.red(pixel);
int blue = Color.blue(pixel);
int green = Color.green(pixel);
Use this to crop your image.
Now to detect the color of the image take a pixel from the square and detect it's color with this.
After finding the RGB value use a simple conditional statement to see if the square is red blue or green.
I got it this way
int topLeftIndex = squareImage.getPixel(0, 0);
int R1 = (topLeftIndex >> 16) & 0xff;
int G1 = (topLeftIndex >> 8) & 0xff;
int B1 = topLeftIndex & 0xff;
and same way with
int bottomLeftIndex=squareImage.getPixel(0, picHeight - 1);
int topRightIndex=squareImage.getPixel(picWidth -1 , 0);
int bottomRightIndex=squareImage.getPixel(picWidth -1, picHieght - 1);

How to remove white background color from BitmapDrawable or bitmap image in android?

I want to remove the white background color in a bitmap like in this example image.
This is my first try:
BitmapDrawable drawableImg = getImage();
drawableImg.setColorFilter(new PorterDuffColorFilter(Color.WHITE,
PorterDuff.Mode.DST_OUT));
Second try:
To remove the white colors range by setting fromBgColor and toBgColor, But when I replace the color with android transparent color the image turns into black here is the code:
public static Bitmap getBitmapWithTransparentBG(Bitmap srcBitmap,
int fromBgColor, int toBgColor) {
Bitmap result = srcBitmap.copy(Bitmap.Config.ARGB_8888, true);
int nWidth = result.getWidth();
int nHeight = result.getHeight();
for (int y = 0; y < nHeight; ++y)
for (int x = 0; x < nWidth; ++x) {
int nPixelColor = result.getPixel(x, y);
if (nPixelColor >= fromBgColor && nPixelColor <= toBgColor)
result.setPixel(x, y, Color.TRANSPARENT);
}
return result;
}
Hint I had a hint that bitmap does not support transparent bits and I should use PNG or Gif instead of bitmaps is that right?
Hint 2 It turned out that bitmap in Android has an option to show transparency since API level 1 using Bitmap.Config enum. Check this link in the documentation of Android.
what is beneath your image? Maybe it could be a layout issue. Have you tried setting android:background="#android:color/transparent" in the xml?
I see you tried the getBitmapWithTransparentBG described in
Android: Make specific (green) color in background image transparent
But have you tried also this?
How to change colors of a Drawable in Android?
About the image being jpg or png, I've managed to work with both after loading and setting it to a imageview (getting the bitmap back from it), and it allows to set the transparency using the getBitmapWithTransparentBG function as quoted above.

Categories

Resources