gpt4 book ai didi

java - 使用Java在谷歌应用程序引擎上逐像素读取和修改图像,几乎就在那里

转载 作者:行者123 更新时间:2023-12-02 08:09:10 24 4
gpt4 key购买 nike

这是我第一次来这里。希望有人能给我们指点!

我们在使用 Java 转换 Google 应用引擎上的图像时遇到了困难。我们基本上想要实现以下目标:

1) 使用 google Chartapi 生成 QRCode - 完成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();
}
}
}

最佳答案

感谢您的帮助。这是我的解决方案:一些有用的链接:BlobStoreGetting 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();
}

关于java - 使用Java在谷歌应用程序引擎上逐像素读取和修改图像,几乎就在那里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7702086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com