Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join 2 images in google app engine in Java

I saw python codes to join two images in google app engine with 'composite'. But I need java codes to use 'composite' to merge two images. Showing an actual code would be very helpful.

like image 437
nanospeck Avatar asked Sep 26 '11 07:09

nanospeck


1 Answers

This is my first reply, so hopefully I won't be hammered too badly. Since nobody else answered this, and I spent a bit of time on this today, I thought I'd provide some code.

The main reason this took way too much time for me, is that for whatever reason, the devserver simulation of the Images API doesn't work right and the composite images are not correct when using the devserver. I was spending forever fiddling with the values in the devserver, until I just uploaded the test code to AppEngine, and it worked as expected. Argg!

Anyway, the code below assumes you have two 300x300 images, one in aImage and other in bImage, that you want to paste side-by-side into a 600x300 new canvas, which is created in the resulting Image newImage:

List<Composite> listComposites=new ArrayList<Composite>();

Composite aPaste = ImagesServiceFactory.makeComposite(aImage, 0, 0, 1f, Composite.Anchor.TOP_LEFT);
listComposites.add( aPaste );

Composite bPaste = ImagesServiceFactory.makeComposite(bImage, 300, 0, 1f, Composite.Anchor.TOP_LEFT);
listComposites.add( bPaste );

Image newImage = imagesService.composite(listComposites, 600, 300, 0xff333333L, ImagesService.OutputEncoding.JPEG);

The first makeComposite places the first image at location 0,0 relative to TOP_LEFT. The second makeComposite places the second image at 300,0. Both are pasted with opacity 1.0. Hope this helps. This code saves the result in JPEG format. And, again, for me, this DOES NOT WORK in the devserver, but works as expected on the real App Engine platform.

like image 169
mrblog Avatar answered Oct 23 '22 10:10

mrblog