how to get a region out of an image? - java

Lets say I use a black key jpg image as my layout in Java Code. Is there an easy way to get the region out of an image so I can use region.Contains() for my onTouchListener?
I've looked for them and there isn't any. I ask this because Im making a piano app and would like to get the regions of the images I use for the black/white keys.

Rather than use an image, try rendering the piano keys yourself, and check if input is in the black key regions you render, rather than using an image.
If you really want to use the image, define the black regions within the image by hand.
Detecting regions in an image is a difficult problem, one you probably don't want to get into for such a simple purpose. If you had many images, or had to do this frequently it might be worth the headache but as you presumably don't... don't worry about it.

Related

Are there any ways to modify a JavaFX Image Object?

I'm showing an Image on a Canvas using JavaFX, but I would like the color of certain parts of the image to vary based on a variable. Ordinarily, I imagine this would be accomplished by creating a color mask and adding that to the image, but as far as I can find JavaFX Image doesn't allow any operations like that, and from the documentation it looks like JavaFX Images can only be loaded from a file. I don't want to modify and then reload the file each time the color changes, because that seems pretty inefficient performance-wise.
My goal is to create an interactive offline app in which a small character is shown, and the user can (for example) use an input to change the characters eye/hair color.
Are there any ways to accomplish this? Essentially doing basic image masking/coloration with JavaFX Images and canvases. (Not ImageView)
Thanks for any advice you can give me!

Java- Draw only in certain areas

I am trying to draw circles (representing people) on a PNG map of Earth. Obviously, I don't want people to be floating in the oceans as the simulated civilization expands. How would I create specified areas on the map that I can draw on? Can I simply restrict the background of the PNG?
I tried using the clip() method, but it still allowed spawning within the oceans. Any suggestions (or if you recommend clip(), how would you use it in this case) would be appreciated.
You somehow have to tell your program where drawing is allowed and where not. There are a bunch of approaches to this:
Try to define a set of features on your PNG image and only spawn (or don't spawn) civilizations if these features are met.
Make a "map" out of your map. Define areas on your PNG image where spawning is allowed and where spawning is not allowed. You can use this using the Polygon class for example.
Make a second PNG image in which you make all oceans purely transparent. Then, only spawn civilizations on the original image at locations where the image with transparency is non-transparent.
Just a bunch of ideas.

find sub-images within an image

I want an efficient way to finding out sub-images from a Image for Ex. we have a image of country map and it contain states as sub-image.
Then i need a way to finding out sub-images of states from country map.
If you have only pixels, you'll need image processing algorithms to find sub-images.
Your solution will be specific to what the image you are processing looks like. For example, if states are outlined in a certain color, you could try edge detection. If states are each different colors, you could run a flood fill like algorithm to create boundaries for each state.
This is a difficult problem. Try computer vision or object recognition for keywords in your research.
However, I suggest instead you build up vector files which define boundaries of subimages by hand. If you've only a few to do, this isn't a big deal.

How can I implement custom maps with provinces in Android?

Ok, so I have this map and I have various sliders on the right. After changing slider values and pressing 'Execute' button, some provinces in the map below should change colour.
However, I don't know how to implement the map below. I have used 33 png drawable for each province. I have set them all to have a same big rectangle dimension so that they'd align themselves.
I am getting an 'Out of memory on byte allocation' error.
I assume this is because of all the large drawables I have.
I'm new to android and I want to ask, is there a way to implement this without the error?
Also the map should always be displayed on the left side of the screen so the images always have to be visible.
I would recommend making a SVG and changing the colors programmatically.
Graphics are hard to scale and are heavy space users, scalable graphics are slim, look great everywhere (device size and dpi) and easy to manage (single file instead of 33).

Scalable clipping mask

I need to to clip variablesized images into puzzle shaped pices like this(not squares): http://www.fernando.com.ar/jquery-puzzle/
I have considered the posibility of doing this with a php library like Cairo or GD, but have little to no experience with these librays, and see no immidiate soulution for creating a clipping mask dynamicaly scalable for different sized images.
I'm looking for guidance/tips on which serverside programing language to use to accomplish this task, and preferably an approach to this problem.
You can create an image using GD with the size of the puzzle piece. and then copy the full image on that image with the right cropping to get the right part of the image.
Then you can just dynamically color in every part of the piece you want to remove with a distinct color (eg #0f0) and then use imagecolorallocatealpha to make that color transparent. Do it for each piece and you have your server side image pieces.
However, if I where you I would create the clipping mask of each puzzle peace in advance in the distinct color. That would make two images per connection (one with the "circle" connecter sticking out and one where this circle connector fits into). That way you can just copy these masks onto the image to create nice edges quickly.
GD is quite complicated, I've heard very good things about Image Magick for which there is a PHP version and lots of documentation on php.net. However, not all web servers would have this installed by default.
http://www.php.net/manual/en/book.imagick.php
If you choose to do it using PHP with GD then the code here may help:
http://php.amnuts.com/index.php?do=view&id=15&file=class.imagemask.php
Essentially what you need to do with GD is to start with a mask at a particular size and then use the imagecopyresampled function to copy the mask image resource to a larger or smaller size. To see what I mean, check out the _getMaskImage method class shown at the url above. A working example of the output can be seen at:
http://php.amnuts.com/demos/image-mask/
The problem with doing it via GD, as far as I can tell, is that you need to do it a pixel at a time if you want to achieve varying opacity levels, so processing a large image could take a few seconds. With ImageMagick this may not be the case.

Categories

Resources