This question already has answers here:
How do I get a Raster from an Image in java?
(2 answers)
Closed 8 years ago.
I have been searching google for stuff like "Java Bitmap", "Create Java Bitmap", etc and cannot seem to find much information. From all of the code examples it looks like all of the bitmap libraries are third party or for android.
What I want to do is very simple. I want to create a small bitmap maybe 10x80, and be able to color each pixel at (x,y). I want to make a small I guess color bar that will show that position of items in a queue by color.
Are there any build in libraries to do this?
There's the java.awt.image.BufferedImage class. This has pixel-specific get/set methods.
http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html
Here is an example of creating and writing to pixels with BufferedImage:
BufferedImage image = new BufferedImage(10, 80, BufferedImage.TYPE_4BYTE_ABGR);
image.setRGB(5, 20, Color.BLUE.getRGB());
The third parameter on the image is the type, which may vary based on your use, but for basic solid colors the one shown is good. The setRGB uses an int representation of the color so you can't just use a Color constant directly.
You probably don't want to use VolatileImage because the benefits are dubious in current Java implementations. See this stackoverflow question for why it may not help.
Look at the Oracle image turorial for help with this. It explains both your options and how to interact with them.
Related
This question already has answers here:
Visualize vector graphics in Java, which library?
(2 answers)
Closed 8 years ago.
I am currently trying to make a game using java, and I wanted to use vector graphics to avoid the blocky feel of many other current games. I've looked into using some of the various Shape implementations in java like Path2D and Area. The problem is is that neither one has all the functionality I need. Here is what I am looking for:
I need to be able to create it using vector graphics based methods (lineTo(), moveTo(), etc.)
I need to be able to draw it onto a JPanel
I need to be able to test whether two instances intersect, or at the very least, whether one contains a point.
Finally, I need to be able to merge them together or subtract one from another.
I know this is kind of a long shot, but I was hoping someone might know of a library or something that has this functionality.
As far as libraries go, I don't have an answer.
I might have missed a point, but as far as I know, Graphics2D Shapes and areas have all the functions you listed. What do they lack you need ?
Of course, JavaFx has all you asked for, but if you want to write a Swing app:
I did not use them for a game, but for an editor, and here is what I would suggest:
create your own graphic element class. Use Shapes and area to implement them, and make them Composite elements.
for collision, have a getArea() method on your elements. The area can be the union of all areas. that represent your element.
in real life, it might be a bit slow, however... Consider doing some pre-rendering as bitmap pictures (maybe when starting the game, when you know the resolution ?)
This question already has answers here:
How to scale an Image in ImageView to keep the aspect ratio
(26 answers)
Closed 9 years ago.
I can load Images from my assets folder just fine, but now I want to scale the Image (originally 400 x 240, want it to be 800 x 480).
I can't do this with Bitmaps because of 2 reasons: (1), can't seem to create Bitmap from a file without knowing the whole path from C:\ to Assets folder, which is dumb since the whole path is gonna change depending on where app is installed; and (2), can't find a way to convert an Image to a Bitmap. And yes, i've looked up on StackOverflow how to try those/similar things, been looking them up and trying again and again for 3 days all day, no luck.
So how to scale Images? I don't understand why it's so hard to manipulate these things, and what is the difference between Bitmap and Image anyway?
Yes, I'm new to Java/Android as of 3 days ago, but I've done a lotta work in C# and other langs. PS, I know how to create and use Canvas and Paint objects. Ideally, I just need a Image scr_scale_image(Image img, int ScaleX, int Scale y) method.
thanks for any help.
try imagemagick, which provides an api for java (JMagick) - http://www.imagemagick.org/script/index.php
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
My use case is pretty simple which I think is shared with many other developers.
I would like to:
1. Load an image
2. Read EXIF Orientation details (If available)
3. Rotate (90, 180, 270) degrees
4. Resize and store images.
I tried going through the forums and then tried:
Sanselan: Reads EXIF, but not JPG images. Also, commons-imaging download links are all broken.
Java ImageIO/Graphics2D: Can rotate/re-size(not one liner, but is understandable) although as suggested you do lose quality when rotating.
*BUT does not read all JPG images (Throws CMMException for some jpg file)
The rest are either too old and not maintained anymore, have no documentation at all or I missed the 'good' one.
Can anyone suggest a library that supports these few 'simple' use cases?
*Using Sanselan to read EXIF is fine. Read/Rotate/Re-size (JPGs) is my main problem
Thumbnailator is a simple Java library which has no external dependencies which can (a) load an image, (b) read the Exif metadata and automatically rotate the image, (c) resize and (d) store the image in one single statement:
Thumbnails.of("path/to/image")
.size(320, 240)
.toFile("path/to/thumbnail");
The above will:
open the image
resize it so that it will fit in a 320 pixel x 240 pixel region, while
maintaining the aspect ratio of the original image, and
properly orient the image according to the Exif metadata, and
store it to a new image
If one desires to perform additional rotations, a rotate method is also available.
Disclaimer: I am the maintainer of Thumbnailator.
Might be much of an overkill if this is the only thing you will have to do, but OpenCV is an excellent image processing library.
It comes with Java bindings, and is under BSD licence.
I say this is an overkill for a simple use case like that, simply because it can do so much more. It is always my first weapon of choice for years though, simply because it has bindings with lots of languages, is portable pretty much everywhere and even now has GPU support :).
There are already SO questions that answer exactly what you want I think. Look here for an example
Hope this helps
You just better use java 2D library itself.
Like :
paint(Graphics g)
Graphics g2= (Graphics)g
g2.draw();
g2.rotate(give parameters)
This question already has answers here:
Merging two images
(4 answers)
Closed 9 years ago.
So I am basically trying to blend two pictures together by taking the weighted average of each pixel. However, in order for the transformation to apply, the two pictures must be exactly the same size. If not, then the first picture remains unchanged.
http://www.seas.upenn.edu/~cis120/current/hw/hw07/javadoc/Manipulate.html#alphaBlend(double,%20NewPic,%20NewPic)
This is basically what I have to do, but I need clarification. I'm not understanding what I have to do with the colors
Firstly, as this is blatantly homework, you should probably check that you're allowed to get code from the internet. If not you should be careful about what you take from answers you get, less you be accused of plagiarism and/or cheating.
Assuming this is fine however, you will want to interpret each NewPic as a bitmap. Then compare each Pixel in each bitmap with the corresponding Pixel in the second bitmap. Look at the average of each colour and use those to create a pixel in a third bitmap. Once you've created each pixel in the third bitmap, return it.
I have an image with 400x400 image to identify different components from it. But when I try to identify components using that most of time it doesn't provide correct answers. So I need to know whether there are some kind of methods in javacv or opencv to improve the quality of the image or increase the size of the image without effecting to its quality ?
This is the sample image that I use. (This is the maximum size that I can get and I can't use any photo editing softwares in the project, because it's dynamically generated image.)
In my image processing I need to identify squares and rectangles that connects those squares. And specially I need to get the width and height of those using pixel values.
You can scale it to any size, if you can vectorize it... and in your case vestorization is quite simple as you have some simple geometrical objects in image.
So, in my view your approach should be like this:
detect edges in the image with a high threshold (as you have very distinct objects)
vectorize them
scale them to any size
You should also look at the following link: Increasing camera capture resolution in OpenCV.
If you stick to image processing the easiest way to do it is to apply an equalizeHist(). This will increase contrast and will improve subsequent steps.
But, and this is a biiiig 'but', why are you doing it? Just reading this post, I saw another solution, and a quick google proved me I am right:
Kabeja is a Java library for parsing, processing and converting
Autodesk's DXF format. You can use Kabeja from the CommandLine or
embed into your application. All parsed data are accessible with the
DOM-like API.
That means you can extract directly all the data you want from that image in a text format. Probably something like "at position x, y there is a transistor, or whatever." So why would you render that file into an image, then analyse that image to extract the components?
If you do it for school (I know that many school projects are like this) I would recommend you to find a real problem to solve, and propose it to your teacher. You will be happier to do something that is not complete nonsense.
vectorizing the image is best option I guess as suggested by mocap.
you can also use enhancement tools like sharpening, saturating etc.