| Author |
j2me: upload an image file captured by cell camera
|
Edward Chen
Ranch Hand
Joined: Dec 23, 2003
Posts: 758
|
|
I am wondering how to upload an image file to web server ? If I have developed a web sevice which will read byte[], in the cell phone, I use J2ME. Say, the cell phone has a camera . After capturing photo, how could the J2ME find that photo file and upload it ? Yes, here we have an assumption, we don't use the cell phone vendor's built-in appliction to upload carema photo. Thanks
|
 |
William Frantz
Ranch Hand
Joined: Dec 08, 2004
Posts: 102
|
|
In general you can't do this. There is no MIDP API that allows you to access the file system such that you could extract photos stored by the native camera application. Some MIDP 2.0 devices have APIs which allow your MIDlet to control the camera and save the pictures that are taken while running your MIDlet, but you still can't access photos stored by the native camera application. There may be some OEM specific extensions that allow this on some phones, but there's no runs-on-anything solution. William Frantz http://sprintdevelopers.com
|
 |
Johan Karlsson
Greenhorn
Joined: Jan 07, 2005
Posts: 26
|
|
Hi! You could use the FileConnection API, if available for your target platform. Many phone from SonyEricsson has this support. I have not looked at any other phones, but I am pretty sure that many other phones also has has the same support. BTW. Regards Johan Jayway
|
 |
Johan Karlsson
Greenhorn
Joined: Jan 07, 2005
Posts: 26
|
|
Hi! BTW. Take a look at this article: http://developers.sun.com/techtopics/mobility/apis/articles/fileconnection/ Regards Johan http://www.jayway.se/
|
 |
Edward Chen
Ranch Hand
Joined: Dec 23, 2003
Posts: 758
|
|
|
I really appreciate your guys responses. It is nice helpful.
|
 |
Swadesh Bera
Greenhorn
Joined: Mar 24, 2005
Posts: 11
|
|
Hi here is some code which may help u to read file from device FileConnection fileConn =(FileConnection)onnector.open "file:///root1/111429594531243BD9-0.jpg", Connector.READ);// file://root1/ is default path for wtk emuluator for other device u need to find the path InputStream fis = fileConn.openInputStream();// open input stream //InputStreamReader fisr = null; byte[] readData = null; long overallSize = fileConn.fileSize(); if ( overallSize > 0 ) { readData = new byte[(int)overallSize]; int bytesRead = 0; int chunckSize = 512; while (bytesRead < overallSize) { int chunck = chunckSize; if ( (overallSize - bytesRead) < chunckSize ) { chunck = (int)(overallSize - bytesRead); } int count = fis.read(readData, bytesRead, chunck); if ( count > 0 ) { bytesRead += count; } } imageData = readData; } System.out.println("imageData"+imageData);
|
 |
Swadesh Bera
Greenhorn
Joined: Mar 24, 2005
Posts: 11
|
|
I am able to upload those image which are less than 2 kb but for bigger image its not possible. So if u find out the solution please share with me
|
 |
Johan Karlsson
Greenhorn
Joined: Jan 07, 2005
Posts: 26
|
|
Which Http request method do you use? HttpConnection.POST? Regards Johan
|
 |
Swadesh Bera
Greenhorn
Joined: Mar 24, 2005
Posts: 11
|
|
Yes, i am using http post request method. But if image size larger than 2048 bytes then http/1.1 response header server : Apache/1.3.31 (Unix) mod_fastcgi/2.4.2 connection : close transfer-encoding : chunked content-type : text/html; charset=iso-8859-1
|
 |
Eduardo Marques
Ranch Hand
Joined: Feb 19, 2005
Posts: 231
|
|
If the device has MMAPI enabled the VideoControl.getSnapShot() method returns an array of bytes that can be sent to the server, without intermediate storage to a file. You may consider this approach unless you are not using MMAPI or really want to store the image data to a local file. For more details see here: http://developers.sun.com/techtopics/mobility/midp/articles/picture/ Quoting from there: "Once the camera video is shown on the device, capturing an image is easy. All you need to do is call VideoControl's getSnapshot() method.You'll need to pass an image type, or null for the default type, PNG. You can find out ahead of time the image types that are supported. The video.snapshot.encodings system property contains a whitespace-delimited list. The Nokia 3650 supports PNG, JPEG and BMP. The getSnapshot() method returns an array of bytes, which is the image data in the format you requested. What you do at this point is up to you: you might save the bytes in a record store, send them to a server, or create an Image from them ... " Regards, Eduardo
|
Eduardo Marques
Get ready for your SCMAD certification at J2MECertificate.com - See what all the buzz is about ...
|
 |
Edward Chen
Ranch Hand
Joined: Dec 23, 2003
Posts: 758
|
|
Thanks. But the problem is , how could I know if it supports MMAPI or not ? Have a website to check which vendor device supports which java API ? Thanks again.
|
 |
Tilman Bender
Greenhorn
Joined: Mar 16, 2008
Posts: 11
|
|
Originally posted by Edward Chen: Thanks. But the problem is , how could I know if it supports MMAPI or not ? Have a website to check which vendor device supports which java API ? Thanks again.
Hi there! Allthough this thread is very old some people might be interested in a partial solution to the above problem. The folks at J2ME-Polish have a device database that you can query for all sorts of properties and even known bugs. The database is open to submissions by registered users.
|
 |
Tim Holloway
Saloon Keeper
Joined: Jun 25, 2001
Posts: 14572
|
|
We don't normally recommend resurrecting long-dead threads, but this is something that's likely to be useful to a lot of people. I actually had to write a camera upload app last year and I did it in .Net and prototyped a J2ME version as well. In the .Net version, the camera wrote a JPEG file and the camera API returned its location. I opened the file and I then transmitted its contents using an HTTP MIME POST, along with some application-specific side data. I don't remember clearly the J2ME equivalent, but I think that the multimedia package returned some sort of stream handle which could be uploaded using the same basic mechanism. We've have a few others on this forum do similar things over the last few months, I think, so if anyone needs to know details, just ask.
|
Customer surveys are for companies who didn't pay proper attention to begin with.
|
 |
L Nyma
Greenhorn
Joined: Sep 26, 2008
Posts: 1
|
|
Hi, I m also having a problem regarding picture upload.. i m exactly doing the same thing ,i.e getting image in jpeg encoding using getsnapshot() and then passing byte[] over stream using HTTP POST, Now my problem is regarding server side. There I m using PHP. but i m unable to get image ,i mean exactly how can i fetch thoese raw bites and turn it in image and move it in a directory. Can anyone help ??
|
 |
Teja Varma Manthena
Greenhorn
Joined: Nov 04, 2011
Posts: 2
|
|
Hi,
I am new to j2me.I want to upload image to server. While I am uploading image from emulator it is hitting the server. But while I am uploading image from mobile it is showing exception.
org.apache.commons.fileupload.FileUploadBase$UnknownSizeException: the request was rejected because its size is unknown
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:305)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest (ServletFileUpload.java:116)
at org.apache.jsp.photo_jsp._jspService(photo_jsp.java:103)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:384)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process (Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Unknown Source)
Here I am sending Mobile and application Number and capture image in bytes `(imagecapturephoto1)`. Capture is storing in view after taking photo.But when we are uploading it is showing exception.
try
{
System.out.println("url:" + serverUrl);
connection = (HttpConnection)Connector.open(serverUrl,Connector.READ_WRITE);
connection.setRequestMethod(HttpConnection.POST);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=*****");
connection.setRequestProperty("User-Agent", "Profile/MIDP-2.1 Configuration/CLDC-1.1");
connection.setRequestProperty("Accept", "application/octet-stream" );
writer = new DataOutputStream(connection.openDataOutputStream());
// writer =new DataOutputStream( conn.openOutputStream());
String name="applicationNumber", name1="mobileNumber",name3="photo",
mimeType="text/plain",mimeType2="image/jpeg";
String value="123456789", value1="9849765432",fileName="applicationphoto.jpeg";
// write boundary
writeString(PREFIX);
writeString(boundary);
writeString(NEWLINE);
// write content header
writeString("Content-Disposition: form-data; name=\"" + name + "\"");
writeString(NEWLINE);
if (mimeType != null)
{
writeString("Content-Type: " + mimeType);
writeString(NEWLINE);
}
writeString("Content-Length: " + applicationNumber.length());
writeString(NEWLINE);
writeString(NEWLINE);
// write content
writeString(applicationNumber);
writeString(NEWLINE);
// write boundary
writeString(PREFIX);
writeString(boundary);
writeString(NEWLINE);
// write content header
writeString("Content-Disposition: form-data; name=\"" + name1 + "\"");
writeString(NEWLINE);
if (mimeType != null)
{
writeString("Content-Type: " + mimeType);
writeString(NEWLINE);
}
writeString("Content-Length: " + mobileNumber.length());
writeString(NEWLINE);
writeString(NEWLINE);
// write content
writeString(mobileNumber);
writeString(NEWLINE);
//uploading image...........
// write boundary
writeString(PREFIX);
writeString(boundary);
writeString(NEWLINE);
// write content header
writeString("Content-Disposition: form-data; name=\"" + name3
+ "\"; filename=\"" + fileName + "\"");
writeString(NEWLINE);
if (mimeType2 != null)
{
writeString("Content-Type: " + mimeType2);
writeString(NEWLINE);
}
writeString("Content-Length: " + imagecapturephoto1.length);
writeString(NEWLINE);
writeString(NEWLINE);
// write content
// SEND THE IMAGE
int index = 0;
int size = 1024;
do
{
System.out.println("write:" + index);
if((index+size)<=imagecapturephoto1.length)
{
writer.write(imagecapturephoto1, index, size);
}
index+=size;
}while(index<imagecapturephoto1.length);
writeString(NEWLINE);
writeString(PREFIX);
writeString(boundary);
writeString(PREFIX);
writeString(NEWLINE);
writer.flush();
//writer.write("-- ***** -- \r\n".getBytes());
serverResponseMessage = connection.getResponseMessage();
InputStream inputstream = connection.openInputStream();
// retrieve the response from server
int chnumber;
StringBuffer sbuffer =new StringBuffer();
while( ( chnumber= inputstream.read() ) != -1 )
{
sbuffer.append( (char)chnumber );
}
String resonsestring=sbuffer.toString();
int end=resonsestring.length();
int tempstr=resonsestring.indexOf(">");
statusResponse=resonsestring.substring(tempstr+1, end);
statusResponse="SUCCESS";
//outputStream.close();
writer.close();
connection.close();
return serverResponseMessage;
}
catch (Exception ex)
{
//statusResponse="SUCCESS";
ex.printStackTrace();
return null;
}
public void writeString(String s) throws java.io.IOException
{
byte[] b = s.getBytes();
writer.write(b, 0, b.length);
}
Please suggest me,How we have to upload image from mobile.I was struck from last 10 days.pleas suggest me how to solve this issue.
Thanks in advance.
-Teja.
|
 |
 |
|
|
subject: j2me: upload an image file captured by cell camera
|
|
|