- Android-er
- Tuesday, October 9, 2012
- Get touched pixel color of scaled ImageView
- 12 comments:
- How do you get the RGB values from a Bitmap on an Android device?
- 5 Answers 5
- Getting the Pixel Color of Image using a Button in Android?
- 2 Answers 2
- Not the answer you’re looking for? Browse other questions tagged java android pixel or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Manipulating images and Drawables with Android’s ColorFilter
- Tinting, custom effects and reusability for visual Android app resources
- Where can this be used? 🤔
- Canvas
- Drawable
- ImageView
- Introducing our sample image 🖼️
- PorterDuffColorFilter 1️⃣
- LightingColorFilter 2️⃣
- ColorMatrixColorFilter 3️⃣
- Limitations and other options 🛠️
- Get background color of a Layout
- 5 Answers 5
- Not the answer you’re looking for? Browse other questions tagged android background-color or ask your own question.
- Linked
- Subscribe to RSS
Android-er
For Android development, from beginner to beginner.
Tuesday, October 9, 2012
Get touched pixel color of scaled ImageView
To get touched pixel color of a ImageView, you can implement OnTouchListener of the ImageView, get touched position by calling event.getX() and event.getY(), and get the pixel color by calling bitmap.getPixel(x, y). IF THE BITMAP IN YOUR ImageView IS DISPLAY AT 1:1, layout_width and layout_height =»wrap_content».
If your bitmap is scaled, such as layout_width and layout_height =»match_parent». It will not work! It’s a example demonstrate how to invert the touched position to get pixel from scaled ImageView.
12 comments:
how can i get pixel frame of image from camera
Thanks really very very useful for me
Nice Post !
Thank you.
You’re doin well ! keep it up
Thank you very very much! you save my life!
On API 19 I am not able to use the Matrix function. Do you know an alternative for that?
hello João Augusto,
API 19 cannot use Matrix? which function?
When I tried to use the command:
Matrix invertMatrix = new Matrix();
I was getting an error «Matrix is deprecated» but after I changed my minSdkVersion to 21 it seems to be fixed.
Thanks. Great Code by the way!
Hello João Augusto,
Which Matrix you include? android.opengl.Matrix? (http://developer.android.com/reference/android/opengl/Matrix.html#Matrix())
What I include is android.graphics.Matrix (http://developer.android.com/reference/android/graphics/Matrix.html#Matrix()).
Try to change your include statement.
Thanks aloot ,I am a developer,It help me alot to complete my project
Thanks man. 2016 and works like a charm!!
Thanks for the above post, however I am trying the same code to pick a color from an image at the touched position. It is not picking the same color at the touched position. Could you please help me to understand what is the problem. Is there any relation between the screen coordinates and image coordinates.
Источник
How do you get the RGB values from a Bitmap on an Android device?
I want to get RGB values of bitmap in android but I cant do this so far. My aim is to obtain RGB values for each pixel of bitmap. Is there any specific function for android or anything else ?? Also I wonder that do I need colorMatrix() function? It is very important for my project. Thanks
5 Answers 5
This may be slightly late, but to clear up the confusion with the use of &0xff:
In Java ints are 32 bits, so the (A)RGB values for each pixel are packed in 4 bytes. In other words, a pixel with the values R(123), G(93), B(49) = FF7B 5D31 in the ARGB_8888 model. Where Alpha = FF, R = 7B, G = 5D, B = 31. But this is stored as an int as -8692431.
So, to extract the Green value from -8692431, we need to shift the 5D by 8 bits to the right, as you know. This gives 00FF 7B5D. So, if we were just to take that value we would be left with 16743261 as our Green value. Therefore, we bitwise-and that value with the mask of 0xFF (which is equivalent to 0000 00FF) and will result in 00FF 7B5D being ‘masked’ to 0000 005D. So we have extracted our Green value of 5D (or 93 decimal).
We can use the same mask of 0xFF for each extraction because the values have all been shifted to expose the desired two bytes as the least significant. Hence the previously suggested code of:
If it makes it clearer, you can perform the equivalent operation of:
For brevity, the extra 0s can be dropped, and it can be written as
Note however, that alternative colour models may be used, such as RGB_555 which stores each pixel as just 2 bytes, with varying precision for the RGB channels. So you should check the model that your bitmap is using before you perform the extraction, because the colours may be stored differently.
Источник
Getting the Pixel Color of Image using a Button in Android?
I’m still learning Android programming, and right now I’m writing an app to detect colors in a picture uploaded from the gallery. So far, using a tutorial, the app can upload a picture from the gallery into imageview, but I’m not sure how to get the second part working. I want the user to be able to simply press a button and display the color of the image (right now I’m using single color images). How would I go about doing this? Here’s my source code so far:
2 Answers 2
get imageview’s bitmap:
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
get color of pixel at location:
int pixel = bitmap.getPixel(x,y);
there you have your color for a specific pixel.
Not the answer you’re looking for? Browse other questions tagged java android pixel or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
Manipulating images and Drawables with Android’s ColorFilter
Tinting, custom effects and reusability for visual Android app resources
Image and Drawable resources are an integral part of any Android app. Since day (i.e. API level) 1, the Android framework has provided a means of manipulating the colors of these assets on a per-pixel level, as they are drawn to screen, with the ColorFilter class.
The documentation for the base abstract class stipulates that “a color filter can be used with a Paint to modify the color of each pixel drawn with that paint”. This is a powerful tool that can be used for:
- Applying effects and adjustments to images
- Tinting of icons and logos
Ultimately this encourages the good practice of reusing resources for scenarios that require different color variations, resulting in reduced app size.
Note: ColorFilter is an abstract class that should never be used directly, and the constructor was deprecated in API level 26. Only the subclasses should be used, which we will discuss further down.
Where can this be used? 🤔
Before we explore the existing subclasses and their capabilities, we need to discuss where we can actually use ColorFilter .
Canvas
As hinted at in the documentation description, we can use ColorFilter when drawing to Canvas in a custom View . Specifically, a ColorFilter is set on a Paint which then affects everything that is drawn with it:
Drawable
A ColorFilter can be applied to a Drawable , which will affect how it is rendered in View s and layouts:
In addition to this, there exists a Drawable convenience function to apply a PorterDuffColorFilter (more on that later):
Lastly, it is important to note that a tint can be applied to a Drawable . This is separate and will be overridden if a ColorFilter is set via either one of the above functions.
ImageView
A ColorFilter can be applied to an ImageView , which will affect how its current image will be rendered:
As with Drawable , convenience functions exist for applying a PorterDuffColorFilter :
Introducing our sample image 🖼️
We are now going to dive into the three subclasses of ColorFilter ( PorterDuffColorFilter , LightingColorFilter and ColorMatrixColorFilter ). In order to demonstrate this, we will make use of a visual aid in the form of a sample image:
It has photographic detail while also having “shape” as a result of the transparent areas. This will allow all of the subclasses to be demonstrated.
PorterDuffColorFilter 1️⃣
We have already touched on this briefly. This is a color filter that accepts a single color that is applied to all source pixels along with a Porter-Duff composite mode. There are many modes that are suitable to different scenarios. Typically, this is used to apply a “blanket” color (eg. To tint an icon).
LightingColorFilter 2️⃣
This color filter can be used to simulate lighting effects on an image. The constructor accepts two parameters, the first to multiply the source color ( colorMultiply ) and the second to add to the source color ( colorAdd ).
While the source color alpha channel is not affected, the R, G and B channels are computed like so:
Note: The above is using Android KTX Color extension functions for accessing the red, blue and green channels (alpha is also available).
ColorMatrixColorFilter 3️⃣
This is arguably the most flexible (but also the most complex) color filter.
It is quite similar to LightingColorFilter , in that we can tweak each pixel’s color channels using values that are multiplicative and additive. However, a ColorMatrixColorFilter is constructed with a ColorMatrix , which is essentially a wrapper class for a 4×5 matrix. This gives us 20 values, used in a certain way, that allow us to transform colors using all of the existing channels, including alpha.
Before we dive into using matrices, let’s first take a look at some of the convenience functions offered by ColorMatrix :
Fear not if some of these color concepts do not make sense! The point here is that ColorMatrix offers a lot of flexibility, and these convenience functions are simply matrix operations under the hood.
We know that ColorMatrix wraps a 4×5 matrix. Given this matrix, how do we arrive at our resultant color channels? They are computed like so:
The 4 rows in fact represent the resultant R, G, B and A channels. For each channel, the 5 columns allow you to combine the existing R, G, B, and A channels and a wildcard value in a plethora of ways.
This provides us with a lot of possibilities. We can adjust the brightness/contrast of images, ignore some channels, invert an image or even create basic filters and effects.
Limitations and other options 🛠️
While ColorFilter is powerful and flexible, image manipulation is a vast field and it simply cannot cover everything.
For example, the current pixel value that is being passed through the filter would be handy to know, but is not available. Additionally, you are limited to the three subclasses that ColorFilter currently provides. It appears as if you cannot create a custom subclass, due to native code restrictions.
In instances like this, what other options do we have?
The graphics framework has other useful classes such as Shader and MaskFilter . We could turn to RenderScript , which offers Bitmap utilities that still keep us mostly within traditional Android graphics territory. There is also OpenGL, which is perhaps the most extreme power vs. complexity tradeoff, but opens up all of the possibilities of custom GLSL shaders.
Overall, ColorFilter is still a fantastic tool for working with app resources.
I hope this post has provided some insight into ColorFilter and the flexibility it provides when working with images and Drawable s. If you have any questions, thoughts or suggestions then I’d love to hear from you!
Источник
Get background color of a Layout
I want to find the background color of a Layout from my code. Is there any way to find it? something like linearLayout.getBackgroundColor() ?
5 Answers 5
This can only be accomplished in API 11+ if your background is a solid color.
To get background color of a Layout:
If It is RelativeLayout then just find its id and use there object instead of LinearLayout.
ColorDrawable.getColor() will only work with API level above 11, so you can use this code to support it from API level 1. Use reflection below API level 11.
Short and Simple way:
For kotlin fans
Not the answer you’re looking for? Browse other questions tagged android background-color or ask your own question.
Linked
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник