I have been researching this for a while on this site and have not came up with exactly what I am looking for. I am working with software in which I do not have the source for must pass that code an Image.class image. I have 2 images that I need to overlay the first one over the second and then pass that combined image to the software I am interfacing with. I must use the Java Language for all code.
Everything I have found is writing the 2 images directly. Is what I am trying to do possible and if so please can you help me get these results? A code stub would be wonderful. I surely appreciate all of the time you take in assisting me.
Basically, you can "paint" one image on to the other...The trick is getting one of them into the correct format...
Image image1 = ...;
Image image2 = ...;
BufferedImage buffer1 = new BufferedImage(image1.getWidth(this), image1.getHeight(this), BufferedImage.TYPE_ARGB);
Graphics2D g = buffer.createGraphics();
g.drawImage(image1, 0, 0, this);
g.drawImage(image2, 0, 0, this);
g.dispose();
This will overlay image2 over image1. You can either assign buffer1 to image1 and pass it to you other class or simple pass buffer1 as BufferedImage extends from java.awt.Image
Related
So It took me quite some time solving one problem I had in my code and I'm very interested in some details. I've written a part what exactly I was doing down at the end.
So i was reading an image which I wanted to use with
static BufferedImage img = null;
img = ImageIO.read(new File("/home/user/doggo.jpg"));
Then created a BufferedImage object to store the changes which I would have done.
BufferedImage newimg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
So because some parts of the image would not be changed, I figured i'd "copy" the BufferedImage to the other one (so there wouldn't be empty places) by doing:
newimg = img;
So when I was running my code, sometimes the image would be distorted, pixelated or not what i hoped for. I knew my algorithm was correct 100% and that there should be no way why it would not work.
It turned out, at least what i lea that I was "copying" one type of BufferedImage to a different type.
img.getType() returned
1
img.getColorModel() returned:
DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0
and img.getSampleModel() returned:
java.awt.image.SinglePixelPackedSampleModel#8080b20
For the newly created BufferedImage I got:
newimg.getType() returned
5
newimg.getColorModel() returned:
ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace#76fb509a transparency = 1 has alpha = false isAlphaPre = false
and newimg.getSampleModel() returned:
java.awt.image.PixelInterleavedSampleModel#3086002
I'm mostly interested in how does ImageIO read the image into a BufferedImage? How does it define what type of BufferedImage it will be. The image i was reading was a normal jpeg file which has RGB values, so I presumed it would not do much harm copying the BufferedImage objects like that. By now I realized that it is not as simple as I imagined but I'm still in the dark about what happened in the background. I tried reading the oracle docs but they seem maybe either too lacking or too abstract for me to comprehend.
As for the stuff I was coding, I was doing kernel convolution with images, like blur, edge detection etc. and I just wanted to copy the source image to the destination one because I didn't do any edge wrapping/clipping and I did not want the edges of the newly created image to be empty.
i Have a rectangle drawn by opencv+java+android. now i need to crop and display in my imagviwe how to crop it. submat methord is allow only int values. but rect.tl().x values are in double. casting is not a good solution. it make errors.
Core.rectangle(ImageMatin, rect.tl(), rect.br(), new Scalar(255, 0, 0),1); // works
Mat cropped = ImageMatin.submat(rect.tl().x, rect.height, rect.tl().x, rect.width); //error
i recommend this library from github: https://github.com/edmodo/cropper , it even has a sample so if you get stuck you can always refer to it. Also this library is great for cropping purposes and lightweight as well, i myself have used it in one or two apps
You can crop Mat images by appling this command:
Mat croppedImg = new Mat (sourceMat, ROI);
I learned to use snapshot to make an Image object out of a node. Having multiple Groups that hold various strokes, I'm now trying to create a single Image with strokes from both groups. For this purpose I'm using the following code:
Group strokes1;
Group strokes2;
WriteableImage im = null;
SnapshotParameters params = new SnapshotParameters();
params.setFill(Color.TRANSPARENT);
params.setViewport(new Rectangle2D(0, 0, 400, 400));
im = strokes1.snapshot(params, im);
im = strokes2.snapshot(params, im);
The documentation for the snapshot function says that
"If the image is non-null, the node will be rendered into the existing image."
However, the resulting Image im only contains strokes from strokes2. What am I doing wrong?
One way to achieve your goal is to use setComposite() method on the Graphics2D of the BufferedImage to be converted, this solution is discussed briefly here and here.
this gist provide a runnable complete example of the approach.
I have been trying to get an Android equivalent code for the following:
private BufferedImage user_space(BufferedImage image)
{
BufferedImage new_img = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D graphics = new_img.createGraphics();
graphics.drawRenderedImage(image, null);
graphics.dispose();
return new_img;
}
I want an android equivalent of Graphics2D. I have been searching for a while and found out that Canvas in android can do some similar tasks as performed by the Graphics2D class here but the main problem is that I'm not getting the android equivalent of the whole process that is being performed in the code.
android does not have BufferedImage or Graphics2D as you probably already found out.
I'm not a java developer (just android) so I'm not 100% sure on what you're trying to achieve, but it seems to me that you're simply creating a copy of the image.
here a few classes that you'll be using for those type of manipulation:
Bitmap: that's like the BufferedImage, it's the object that actually stores all the bytes from the image (possibly large object with potential for memory crash). There're some static methods here for copy bitmaps, create new mutable or immutable bitmaps, scale, or compress as PNG or JPG to a stream.
BitmapFactory: lots of static methods to create new bitmaps from resources, files, streams, etc.
Canvas: You'll actually only use this one if you want to draw on the image (overlay image, put some text, or a line, stuff like that)
I have a problem I've been stuck on for a few days. I am trying to convert a nine patch image into a bitmap array, and change a specific color to a different one. I am having trouble converting the bitmap back to a nine patch, so I can use it as a background for a layout. I have tried using this code to create the bitmap then convert it back to a nine-patch drawable, but it just starts the activity and blinks black then goes back to the parent activity. If I comment out the creation and initialization of the nine-patch the activity starts just fine.
Bitmap backMap = BitmapFactory.decodeResource(getResources(),R.drawable.outerbackground);
backMap = backMap.copy(Bitmap.Config.ARGB_8888, true);
backMap = getChangedColor(backMap, Color.rgb(212, 212, 212), Color.rgb(55, 50, 255));
byte[] chunk = backMap.getNinePatchChunk();
NinePatchDrawable np_drawable = new NinePatchDrawable(getResources(), backMap, chunk, new Rect(), null);
np_drawable.setBounds(0, 0, backMap.getWidth(), backMap.getHeight());
I usually don't ask for help on such a thing, but I have went through all the documentation I could find on nine-patch images and bitmaps and still can't figure it out.
Long question short, I need to know how to convert a bitmap to a nine-patch image for use as a background. Thanks a million in advance. you'll get the other million after your done.
O and I am targeting version 16 with the minimum of 8.