Permalink
Browse files

demo now supports image upload

  • Loading branch information...
ricardocabral committed Jan 23, 2012
1 parent 96b6801 commit 2b4c57831645b3bf53953767c8354f2603c0a0b7
View
@@ -8,5 +8,9 @@
<classpathentry kind="lib" path="war/WEB-INF/lib/ws-commons-util-1.0.2.jar"/>
<classpathentry kind="lib" path="war/WEB-INF/lib/xmlrpc-client-3.1.3.jar"/>
<classpathentry kind="lib" path="war/WEB-INF/lib/xmlrpc-common-3.1.3.jar"/>
<classpathentry kind="lib" path="war/WEB-INF/lib/gwtupload-0.6.4.jar"/>
<classpathentry kind="lib" path="war/WEB-INF/lib/commons-fileupload-1.2.1.jar"/>
<classpathentry kind="lib" path="war/WEB-INF/lib/log4j-1.2.13.jar"/>
<classpathentry kind="lib" path="war/WEB-INF/lib/commons-io-1.4.jar"/>
<classpathentry kind="output" path="war/WEB-INF/classes"/>
</classpath>
@@ -12,7 +12,10 @@
<!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/> -->
<!-- Other module inherits -->
<!-- Include GWTUpload library -->
<inherits name="gwtupload.GWTUpload"/>
<!-- Load dinamically predefined styles in the library when the application starts -->
<stylesheet src="Upload.css"/>
<!-- Specify the app entry point class. -->
<entry-point class='net.imgseek.server.demo.client.Iskdaemon_demo'/>
@@ -20,6 +20,13 @@
*/
package net.imgseek.server.demo.client;
import gwtupload.client.IUploadStatus.Status;
import gwtupload.client.IUploader;
import gwtupload.client.IUploader.OnFinishUploaderHandler;
import gwtupload.client.IUploader.UploadedInfo;
import gwtupload.client.MultiUploader;
import java.util.ArrayList;
import java.util.List;
import net.imgseek.server.demo.shared.DbImageResult;
@@ -46,6 +53,7 @@
public class Iskdaemon_demo implements EntryPoint, ValueChangeHandler<String> {
private static final int GRID_COLS = 4;
private static final int NUMRES = 12;
private static final NumberFormat df = NumberFormat.getFormat("#.##");
/**
* Create a remote service proxy to talk to the server-side Isk service.
@@ -54,6 +62,28 @@
.create(IskDemoService.class);
private final Grid resultsGrid = new Grid(3, GRID_COLS);
private final MultiUploader uploaderControl = new MultiUploader();
private OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {
public void onFinish(IUploader uploader) {
if (uploader.getStatus() == Status.SUCCESS) {
// The server sends useful information to the client by default
UploadedInfo info = uploader.getServerInfo();
List<DbImageResult> result = new ArrayList<DbImageResult>();
String ps[] = info.message.split(";");
for (int i = 0; i < ps.length / 3; i++) {
DbImageResult nr = new DbImageResult();
nr.setId(Integer.parseInt(ps[i * 3]));
nr.setScore(Double.parseDouble(ps[i * 3 + 1]));
nr.setUrl(ps[i * 3 + 2]);
result.add(nr);
}
showResults(result);
uploaderControl.reset();
}
}
};
/**
* This is the entry point method.
@@ -63,11 +93,20 @@ public void onModuleLoad() {
RootPanel.get("main").add(mvp);
HorizontalPanel hhp = new HorizontalPanel();
mvp.add(hhp);
hhp.add(new HTML("<h1>isk-daemon example</h1>"));
hhp.add(new HTML("<h2>isk-daemon example</h2>"));
hhp.add(new HTML(
"<p><a href=http://server.imgseek.net/>isk-daemon</a> is an open source database server capable of adding content-based (visual) image searching to any image related website or software. Click on the '+ similar' link below each image to search by visual similarity. See <a href='http://www.imgseek.net/isk-daemon/demo'>more details</a> about this demo, including source code.</p>"));
"<p><a href=http://server.imgseek.net/>isk-daemon</a> is an open source database server capable of adding content-based (visual) image searching to any image related website or software. Click on the '+ similar' link below each image to search by visual similarity. You can also upload your own image by clicking on the 'Choose file' button below. See <a href='http://www.imgseek.net/isk-daemon/demo'>more details</a> about this demo, including source code.</p>"));
hhp.add(new Hyperlink("Random images", "similar-0"));
// Create a new uploader panel and attach it to the document
uploaderControl.setAutoSubmit(true);
mvp.add(uploaderControl);
// Add a finish handler which will load the image once the upload
// finishes
uploaderControl.addOnFinishUploadHandler(onFinishUploaderHandler);
mvp.add(resultsGrid);
mvp.add(new HTML(
"<br/>Copyright 2012 <a href=http://www.imgseek.net/>imgSeek project</a>. Sample images are copyrighted by <a href=http://www.vision.caltech.edu/Image_Datasets/Caltech256/>Caltech 256</a>. Includes 30,607 images, covering a large number of categories."));
@@ -83,6 +122,27 @@ public void onModuleLoad() {
History.fireCurrentHistoryState();
}
private void showResults(List<DbImageResult> result) {
resultsGrid.clear();
int i = 0;
for (DbImageResult res : result) {
VerticalPanel rvp = new VerticalPanel();
rvp.setStyleName("dotted");
rvp.add(new Image(res.getUrl()));
HorizontalPanel hc = new HorizontalPanel();
String scoreLabel = "(random) ";
if (res.getScore() > 0)
scoreLabel = df.format(res.getScore()) + "%";
hc.add(new Label(scoreLabel));
hc.add(new Hyperlink(" + similar", "similar-" + res.getId()));
rvp.add(hc);
resultsGrid.setWidget(i / GRID_COLS, i % GRID_COLS, rvp);
i++;
}
}
@Override
public void onValueChange(ValueChangeEvent<String> event) {
int tid = 0;
@@ -92,36 +152,16 @@ public void onValueChange(ValueChangeEvent<String> event) {
e.printStackTrace();
}
final NumberFormat df = NumberFormat.getFormat("#.##");
iskDemoService.queryImgID(tid, NUMRES, false,
new AsyncCallback<List<DbImageResult>>() {
public void onFailure(Throwable caught) {
// TODO Show the RPC error message to the user
}
public void onSuccess(List<DbImageResult> result) {
resultsGrid.clear();
int i = 0;
for (DbImageResult res : result) {
VerticalPanel rvp = new VerticalPanel();
rvp.setStyleName("dotted");
rvp.add(new Image(res.getUrl()));
HorizontalPanel hc = new HorizontalPanel();
String scoreLabel = "(random) ";
if (res.getScore() > 0)
scoreLabel = df.format(res.getScore()) + "%";
hc.add(new Label(scoreLabel));
hc.add(new Hyperlink(" + similar", "similar-"
+ res.getId()));
rvp.add(hc);
resultsGrid.setWidget(i / GRID_COLS, i % GRID_COLS,
rvp);
i++;
}
showResults(result);
}
});
}
}
}
@@ -0,0 +1,29 @@
package net.imgseek.server.demo.server;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
public class IskDaemonClient {
public XmlRpcClient client;
public IskDaemonClient() {
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
try {
config.setServerURL(new URL("http://127.0.0.1:31128/RPC"));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // isk-daemon
// XML-RPC
// endpoint
// URL
config.setEnabledForExtensions(true);
this.client = new XmlRpcClient();
this.client.setConfig(config);
}
}
@@ -20,8 +20,6 @@
*/
package net.imgseek.server.demo.server;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@@ -30,8 +28,6 @@
import net.imgseek.server.demo.shared.DbImageResult;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
@@ -43,23 +39,13 @@
IskDemoService {
private static final int DB_NUM_IMGS = 30607; // total number of images on
// DB. Used for quickly
// selecting random ids
private XmlRpcClient client;
private ImageDb imgIdDb = new ImageDb();
private IskDaemonClient iskClient = new IskDaemonClient();
private Random generator;
private ImageDb imgIdDb = new ImageDb();
public IskDemoServiceImpl() throws MalformedURLException {
public IskDemoServiceImpl() {
super();
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://127.0.0.1:31128/RPC")); // isk-daemon
// XML-RPC
// endpoint
// URL
config.setEnabledForExtensions(true);
client = new XmlRpcClient();
client.setConfig(config);
generator = new Random(1958027);
}
@@ -83,7 +69,7 @@ public DbImageResult whitelist(DbImageResult l) {
// searching by default on DB Space 1
Object[] params = new Object[] { 1, (int) id, numres, fast };
try {
Object[] result = (Object[]) client.execute("queryImgID",
Object[] result = (Object[]) iskClient.client.execute("queryImgID",
params);
for (Object res : result) {
Object[] r = (Object[]) res;
@@ -0,0 +1,119 @@
package net.imgseek.server.demo.server;
import gwtupload.server.UploadAction;
import gwtupload.server.exceptions.UploadActionException;
import gwtupload.shared.UConsts;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.imgseek.server.demo.shared.DbImageResult;
import org.apache.commons.fileupload.FileItem;
import org.apache.xmlrpc.XmlRpcException;
/**
* This class sends by email, all the fields and files received by GWTUpload
* servlet.
*
* @author Manolo Carrasco Moñino
*
*/
public class UploadServlet extends UploadAction {
private IskDaemonClient iskClient = new IskDaemonClient();
private static final long serialVersionUID = 1L;
Hashtable<String, String> receivedContentTypes = new Hashtable<String, String>();
/**
* Maintain a list with received files and their content types.
*/
Hashtable<String, File> receivedFiles = new Hashtable<String, File>();
private ImageDb imgIdDb = new ImageDb();
/**
* Override executeAction to save the received files in a custom place and
* delete this items from session.
*/
@Override
public String executeAction(HttpServletRequest request,
List<FileItem> sessionFiles) throws UploadActionException {
String response = "";
List<DbImageResult> dbImageList = new ArrayList<DbImageResult>();
int cont = 0;
for (FileItem item : sessionFiles) {
if (false == item.isFormField()) {
cont++;
try {
// / Create a new file based on the remote file name in the
// client
// String saveName =
// item.getName().replaceAll("[\\\\/><\\|\\s\"'{}()\\[\\]]+",
// "_");
// File file =new File("/tmp/" + saveName);
// / Create a temporary file placed in /tmp (only works in
// unix)
// File file = File.createTempFile("upload-", ".bin", new
// File("/tmp"));
// / Create a temporary file placed in the default system
// temp folder
byte[] data = item.get();
// searching by default on DB Space 1
Object[] params = new Object[] { 1, data, 12, false };
try {
Object[] result = (Object[]) iskClient.client.execute(
"queryImgBlob", params);
for (Object res : result) {
Object[] r = (Object[]) res;
int rid = (Integer) r[0];
response += (rid + ";");
response += (r[1] + ";");
response += (this.imgIdDb.getUrlForImg(rid) + ";");
}
// return "";
} catch (XmlRpcException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
throw new UploadActionException(e.getMessage());
}
}
}
// / Remove files from session because we have a copy of them
removeSessionFileItems(request);
// / Send your customized message to the client.
return response;
}
/**
* Get the content of an uploaded file.
*/
@Override
public void getUploadedFile(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String fieldName = request.getParameter(UConsts.PARAM_SHOW);
File f = receivedFiles.get(fieldName);
if (f != null) {
response.setContentType(receivedContentTypes.get(fieldName));
FileInputStream is = new FileInputStream(f);
copyFromInputStreamToOutputStream(is, response.getOutputStream());
} else {
renderXmlResponse(request, response, XML_ERROR_ITEM_NOT_FOUND);
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.

0 comments on commit 2b4c578

Please sign in to comment.