这是我第一次来这里。希望有人能给我们一个提示!
我们在使用Java在Google应用引擎上转换图片时遇到困难。我们 基本上想要实现以下目标:
1)使用google chartapi生成QRCode - DONE 2)使用urlfetch获取刚刚生成的qrcode并使用pngw / pngr (用于appengine的图像库)来读取和修改上的像素 图像 - 完成
现在我们不知道如何:
3)将修改后的图像保存在blobstore上,然后才能显示 使用blobstore api的屏幕。 *我们在本地使用库并在本地保存C:\ test.png有效 很好。
代码如下: *我们使用了一个pngr库,它使用InputStream作为PngReader 而不是文件。它适用于App Engine,用于读取和修改像素 来自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();
}
}
}
答案 0 :(得分:1)
鉴于this part of the documentation,此代码可以帮助您入门:
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.
答案 1 :(得分:1)
感谢您的帮助。这是我的解决方案: 一些有用的链接:BlobStore和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();
}