B4J Question B4J - General Question - Interrogating a Spot on the Screen

AlexOfOz

Active Member
Licensed User
This is a general question.

Is there a way of interrogating a spot on the Windows screen to find it's colour?

Alternatively, is it possible to click on a spot on an ImageView to find its colour?

Thanks,
Alex
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Alternatively, is it possible to click on a spot on an ImageView to find its colour?
Yes. Find the coordinate and get the pixel value with Image.GetPixel.

If the image is scaled then you will need to do some simple math.

Is there a way of interrogating a spot on the Windows screen to find it's colour?
You can use this library to take a screenshot of a region on the screen and then get the pixel value: https://www.b4x.com/android/forum/threads/jawtrobot-invoke-keyboard-and-mouse-events-etc.55832
 
Upvote 0

AlexOfOz

Active Member
Licensed User
I'm sad to say, but I just cannot figure it out.

Can someone please post a simple sample code to get the colour of a pixel from an ImageView, and then explain how to use it. I'm obviously doing something very simple and very wrong, because all I get is a value of -1.

My section of code that is not working is -

xspot = EventData.X
yspot = EventData.Y

pixelsample = ImageView.GetImage.GetPixel(xspot, yspot)

- where pixelsample is an INT.

Thank-you
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Can someone please post a simple sample code
Start with posting a simple example showing the problem.
We can not guess what you are doing, where the image comes from ect.

The Screencoordinates can only reflect the image position if the image is at Position 0x0 and is fullscreen.

If that is not the case then you need to convert the coodinates to the forms imagesview coordinates (Math needed).
 
Upvote 0

AlexOfOz

Active Member
Licensed User
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private backimage As ImageView
    Private xspot, yspot As Float
    Private backpane As Pane
    Private pixelsample As Byte

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
End Sub

Private Sub backimage_MouseClicked (EventData As MouseEvent)
  
    xspot = EventData.X
    yspot = EventData.Y
  
    pixelsample = backimage.GetImage.GetPixel(xspot, yspot)
  
    Log("pixelsample = " & pixelsample)
  
End Sub

As I say, a very simple code for the sole purpose of learning how to get the colour of a pixel in an ImageView.

Thanks DonManfred. Oh, and on your advice I was now making full screen, but same result.

Alex
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Try this:
B4X:
Private Sub backimage_MouseClicked (EventData As MouseEvent)
 
    xspot = EventData.X
    yspot = EventData.Y
 
    pixelsample = backimage.Snapshot.GetPixel(xspot, yspot)
 
    Log("pixelsample = " & pixelsample)
 
End Sub

Attached my test program, works also when moving the mouse.
Be aware that GetImage returns the original bitmap, not the displayed bitmap.
To get the displayed bitmap you need to use Snapshot.

There is an error in your code.
You say that pixelsample is an Int, but you declare it as a Byte !?
 

Attachments

  • GetPixel.zip
    30.9 KB · Views: 84
Upvote 0

AlexOfOz

Active Member
Licensed User
pixelsample is not a byte, it is a int of 4bytes
Thanks teddybear; I was worried that might cause some confusion. I tried every type of number format, including INT. That just happened to be the last one I tried before posting.

I am now working with the sample code that klaus provided and am tripping over B4XView. I'll keep trying until I get it right. I must be missing a library.
 
Upvote 0

AlexOfOz

Active Member
Licensed User
Thank-you to all.

By using the help from each of you and adding it to my growing bucket of knowledge, I have been able to achieve what I was trying to achieve. I still have a lot of work to do, but with your help I'm back on track.

BTW, I now know that the thing that was tripping me up by confusion was that when I print a LOG of the detected colour, it often shows simply as -1. But if I plug that value in to the CSSUTILS.SetBackgroundColor command regardless, it works.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I am now working with the sample code that klaus provided and am tripping over B4XView. I'll keep trying until I get it right. I must be missing a library.
To use B4XViews, you need to check the jXUI library in the Libraries Manager Tab.
You may have a look at the B4X XUI booklet to see the advantages of using B4XViews.
I used the Label as a B4XView because it has a Color property.
 
Upvote 0

AlexOfOz

Active Member
Licensed User
UPDATE

With the needed and helpful input from this forum, I have solved my problem and learned a lot along the way, which will help as I progress with my learning and experimenting.

I now understand what was meant by the comment above from DonManfred of maths being needed to make the conversion. I have learned how to make the conversion and it is now working as hoped for. And yes, it was sneaky and a little bit tricky.

So finally, thank-you all.
 
Upvote 0
Top