Hey everyone,
I'm making an app that is going very well, but I have a problem uploading the images to my webserver. The way I work is as the following:
The code repeatedly takes a snapshot from the camera preview using setOneShotPreviewCallback() to call onPreviewFrame(). The frame is delivered in YUV format so raw2jpg() converts it into 32 bit ARGB for the jpeg encoder.
getPicture() is called, presumably by the application, and produces the jpeg data for the image in the private byte array mCurrentFrame and returns that array. Next I upload the picture to my index.php file with public void uploadFrame().
I use the following code in my java library:
My code uploadFrame is the following: Android: Upload image to server with the public void uploadFrame. but because in there, they use a bitmap and not a jpg file, I think that this is the problem that it doesn't display on my webserver.
This is the php server side:
So is there any code that will convert my raw data to a bmp file instead of a jpg file (= just like raw2jpg(temp, data, width, height); but then raw2bmp(temp, data, width, height) or is there anything else wrong with the code? It has worked one time with this code, but the picture shown on my webserver was completely green and blurry. This is an example of how it sometimes look like:
I'm making an app that is going very well, but I have a problem uploading the images to my webserver. The way I work is as the following:
The code repeatedly takes a snapshot from the camera preview using setOneShotPreviewCallback() to call onPreviewFrame(). The frame is delivered in YUV format so raw2jpg() converts it into 32 bit ARGB for the jpeg encoder.
getPicture() is called, presumably by the application, and produces the jpeg data for the image in the private byte array mCurrentFrame and returns that array. Next I upload the picture to my index.php file with public void uploadFrame().
I use the following code in my java library:
B4X:
public synchronized void getPicture(int Width, int Height){
FrameWidth = Width;
FrameHeight = Height;
try {
Log.i("B4A", "Try Mode.");
while (!isPreviewOn) wait();
Log.i("B4A", "IsPreviewOn is True");
isDecoding = true;
Log.i("B4A", "Decoding is true.");
c.setOneShotPreviewCallback(this);
Log.i("B4A", "Callback Done.");
} catch (Exception e) {
Log.i("B4A", "An error occured.");
}
}
@Override
public synchronized void onPreviewFrame(byte[] data, Camera camera) {
Log.i("B4A", "In onPreviewFrame");
int width = FrameWidth;
int height = FrameHeight;
int[] temp = new int[width * height];
OutputStream out = new ByteArrayOutputStream();
Bitmap bm = null;
Log.i("B4A", "Initialize raw2jpg");
raw2jpg(temp, data, width, height);
bm = Bitmap.createBitmap(temp, width, height, Bitmap.Config.RGB_565);
bm.compress(CompressFormat.JPEG, 100, out);
mCurrentFrame = ((ByteArrayOutputStream) out).toByteArray();
isDecoding = false;
Log.i("B4A", "isDecoding false");
ba.raiseEvent(this, eventName + "_previewtaken", new Object[] { mCurrentFrame } );
}
/**
* Encodes a bytearray frame with Base64 and uploads it to a web client that supports PHP.
* @param WebClient a string referring to your base.php file (http://IP:Port/base.php)
* e.g. "http://192.168.1.100/android/base.php, "http://www.mywebsite.com/index.php
*
*/
public void uploadFrame() {
Log.i("B4A", "UploadFrame Called");
String ba1=Base64.encodeBytes(mCurrentFrame);
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://www.streamnation.netai.net/index.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
/**
* Converts raw data to jpg.
*
*/
public void raw2jpg(int[] rgb, byte[] raw, int width, int height) {
Log.i("B4A", "In raw2jpg.");
final int frameSize = width * height;
for (int j = 0, yp = 0; j < height; j++) {
int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
for (int i = 0; i < width; i++, yp++) {
int y = 0;
if (yp < raw.length) {
y = (0xff & ((int) raw[yp])) - 16;
}
// int y = (0xff & ((int) raw[yp])) - 16;
if (y < 0)
y = 0;
if ((i & 1) == 0) {
if (uvp < raw.length) {
v = (0xff & raw[uvp++]) - 128;
u = (0xff & raw[uvp++]) - 128;
}
}
int y1192 = 1192 * y;
int r = (y1192 + 1634 * v);
int g = (y1192 - 833 * v - 400 * u);
int b = (y1192 + 2066 * u);
if (r < 0)
r = 0;
else if (r > 262143)
r = 262143;
if (g < 0)
g = 0;
else if (g > 262143)
g = 262143;
if (b < 0)
b = 0;
else if (b > 262143)
b = 262143;
rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000)
| ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
}
}
Log.i("B4A", "Done with raw2jpg");
}
My code uploadFrame is the following: Android: Upload image to server with the public void uploadFrame. but because in there, they use a bitmap and not a jpg file, I think that this is the problem that it doesn't display on my webserver.
This is the php server side:
Got an error when I try to upload with my php code.
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
See this here for my php code: site
So is there any code that will convert my raw data to a bmp file instead of a jpg file (= just like raw2jpg(temp, data, width, height); but then raw2bmp(temp, data, width, height) or is there anything else wrong with the code? It has worked one time with this code, but the picture shown on my webserver was completely green and blurry. This is an example of how it sometimes look like:
Last edited: