reading and modifying images pixel by pixel on google app engine with Java, nearly there
that's my first time here. Hope someone can give us a hint!
We are stuck while transforming images on google app engine with Java. We basically want to achieve the following:
1) Generate a QRCode using google chartapi - DONE 2) Use urlfetch to get the qrcode just generated and use pngw/pngr (image library for appengine) to read and modify the pixels on the image - DONE
Now we have no idea how to:
3) save the modified image on a blobstore to then be able to show on screen using blobstore api. *we used the library locally and saving locally C:\test.png works just fine.
The code is below: * We have used a pngr library which use InputStream for the PngReader instead of File. It works App Engine for reading and modifying pixel by pixel data from a PNG. http://github.com/jakeri/pngj-for-Google-App-Engine
package com.qrcode.server;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.vobject.appengine.java.io.InputStream;
import ar.com.hjg.pngj.ImageLine;
import ar.com.hjg.pngj.PngReader;
import ar.com.hjg.pngj.PngWriter;
public class QrTest extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
try {
URL url = new URL("http://chart.apis.google.com/chart?
cht=qr&chs=400x400&chl=http://google.com&chld=L%7C0");
PngReader pngr;
pngr = new PngReader(url.openStream());
PngWriter pngw = new PngWriter("Name", pngr.imgInfo);
pngw.setOverrideFile(true); // allows to override writen file if
it already exits
//pngw.prepare(pngr); // not necesary; but this can copy some
informational chunks from original
int channels = pngr.imgInfo.channels;
if(channels<3) throw new RuntimeException("Only for truecolour
images");
for (int row = 0; row < pngr.imgInfo.rows; row++) {
ImageLine l1 = pngr.readRow(row);
for(int j=0;j<pngr.imgInfo.cols;j++){
String color_filter = Long.toHexString(l1.getPixelRGB8(j));
if (color_filter.equals("0")){
// CHANGE THE COLOR FOR EACH PIXEL (ROW X COLUMN)
l1.scanline[j*channels]= 250;
//SHOW THE HEX COLOR FOR EACH PIXEL OF THE IMAGE
String out = row +" x " + j +" - " +
Long.toHexString(l1.getPixelRGB8(j));
response.getWriter().println(out);
//SET THE NEW COLOR FOR EACH COLUMN IN
}else{
String out = " ==== NOT BLACK ===";
out ="\n"+ row +" x " + j +" - " +
Long.toHexString(l1.getPixelRGB8(j));
response.getWriter().println(out);
}
}
//pngw.writeRow(l1);
}
pngr.end();
pngw.end();
} catch (MalformedURLException e) {
开发者_如何转开发 // TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Thanks for your help. Here is my Solution: Some helpful links: BlobStore and Getting Image from BlobKey
@Override
public String createImage(String origFilename) {
BlobKey blobKey = null;
String modifiedURL=null;
try {
// Get a file service
FileService fileService = FileServiceFactory.getFileService();
// Create a new Blob file with mime-type "image/png"
AppEngineFile file1 = fileService.createNewBlobFile("image/png");
boolean lock = true;// This time lock because we intend to finalize
// Open a channel to write to it
FileWriteChannel writeChannel = fileService.openWriteChannel(file1, lock);
OutputStream os = Channels.newOutputStream(writeChannel);
//Fetching image from URL
URL url = new URL(escapeHTML(origFilename)); //escape Special Characters
PngReader pngr = new PngReader(url.openStream());
//Create PngWriter to write to Output Stream
PngWriter pngw = new PngWriter(os, pngr.imgInfo);
//Modify the image
int channels = pngr.imgInfo.channels;
if(channels<3) throw new RuntimeException("Only for truecolour images");
for (int row = 0; row < pngr.imgInfo.rows; row++) {
ImageLine l1 = pngr.readRow(row);
for(int j=0;j<pngr.imgInfo.cols;j++)
l1.scanline[j*channels]=250; // Change the color of the pixel
pngw.writeRow(l1); //write rows
}
// Now finalize
pngr.end();
pngw.end();
os.close(); // close the output stream
writeChannel.closeFinally();
//Get the BlobKey
blobKey= fileService.getBlobKey(file1);
/*Using ImageService to retrieve Modified Image URL
http://code.google.com/appengine/docs/java/images/overview.html
*/
ImagesService imagesService = ImagesServiceFactory.getImagesService();
modifiedURL= imagesService.getServingUrl(blobKey);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return modifiedURL;
}
//This is the function to escape special characters
public static final String escapeHTML(String s) {
StringBuffer sb = new StringBuffer();
int n = s.length();
for (int i = 0; i < n; i++) {
char c = s.charAt(i);
switch (c) {
case '|':
sb.append("%7C");
break;
case ' ':
sb.append("%20");
break;
default:
sb.append(c);
break;
}
}
return sb.toString();
}
Given this part of the documentation, this code should get you started:
FileService fileService = FileServiceFactory.getFileService();
// Create a new Blob file with mime-type "image/png"
AppEngineFile file = fileService.createNewBlobFile("image/png");
// Open a channel to write to it
boolean lock = false;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
OutputStream os = Channels.newOutputSTream(writeChannel);
// TODO : wrap the os OutputStream into a PngWriter and write the image
out.close();
writeChannel.closeFinally();
// ...
// Now read from the file using the Blobstore API
BlobKey blobKey = fileService.getBlobKey(file);
// TODO serve the blob using the blob key.
精彩评论