[android-developers] how to use setPreviewCallbackWithBuffer() for real-time camera processing
hi I am new to android sdk. I need to write a program to process the camera image frames. I found "setPreviewCallbackWithBuffer()" from the sdk documentation, but did not figure out how to get it to work. Here is my code skeleton: public void onResume() { super.onResume(); camera=Camera.open(); ... param.setPreviewSize(preview_size.width,preview_size.height); camera.setParameters(param); camera.addCallbackBuffer(new byte[bufSize]); camera.setPreviewCallbackWithBuffer(bufPreviewCallback); camera.startPreview(); } PreviewCallback bufPreviewCallback = new PreviewCallback() { public void onPreviewFrame(byte[] data, Camera camera) { TextView tv = (TextView)findViewById(R.id.txReading); tv.setText("frame done"); } }; I was hoping that the bufPreviewCallback() function gets triggered every time a frame is available, activated right after the startPreview() call. However, when I started the program, my txReading textview never got updated. Can anyone tell me if this is the right way to work with setPreviewCallbackWithBuffer? do I need to call startPreview() inside bufPreviewCallback, like the case for takePicture? thanks Qianqian -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] how to use setPreviewCallbackWithBuffer() for real-time camera processing
On 07/04/2011 06:43 PM, Qianqian Fang wrote: hi I am new to android sdk. I need to write a program to process the camera image frames. I found "setPreviewCallbackWithBuffer()" from the sdk documentation, but did not figure out how to get it to work. Here is my code skeleton: public void onResume() { super.onResume(); camera=Camera.open(); ... param.setPreviewSize(preview_size.width,preview_size.height); camera.setParameters(param); camera.addCallbackBuffer(new byte[bufSize]); camera.setPreviewCallbackWithBuffer(bufPreviewCallback); camera.startPreview(); } PreviewCallback bufPreviewCallback = new PreviewCallback() { public void onPreviewFrame(byte[] data, Camera camera) { TextView tv = (TextView)findViewById(R.id.txReading); tv.setText("frame done"); } }; I was hoping that the bufPreviewCallback() function gets triggered every time a frame is available, activated right after the startPreview() call. However, when I started the program, my txReading textview never got updated. using logcat, I figured out why the callback wasn't triggered: My bufSize was 0 because of pixelinfo.bitsPerPixel wasn't initialized. Now I have another problem: the bufPreviewCallback function was only called once. Is it expected? I appended camera.startPreview() inside bufPreviewCallback, but the log message still only print once. How can I call a function at a rate matching the preview frame update speed (24Hz?) thanks Qianqian Can anyone tell me if this is the right way to work with setPreviewCallbackWithBuffer? do I need to call startPreview() inside bufPreviewCallback, like the case for takePicture? thanks Qianqian -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] set camera focus distance programmatically in the FOCUS_FIXED mode
hi everyone I need to let the camera to focus to a user supplied distance. In the SDK, I only found getFocusDistance to inquire the focus distance, but not one to set the focus distance manually. Can someone give me a hint how to achieve this? thanks Qianqian -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] disabling camera automatic intensity adjustment
hi developers I am working on a camera program with my android phone. The camera takes images through preview buffer. The scene switches between two lighting conditions, one brighter, one weaker. I found that when the lighting condition changes, the camera will adjust image intensity (I guess via exposure time) automatically. This process can take 2 seconds. Because my goal is to take absolute images of the two scenes at the highest speed possible, this automatic intensity adjustment is not desired. I am wondering in the android library, is there a camera parameter I can set to disable this feature? thanks Qianqian -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] setAutoExposureLock method is not found in ICS
hi list I am working on a camera program where I want to call setAutoExposureLock() is supported. To make this backward compatible, I used some sample code I found online to check the presence of this method first. The code snippet is below: ... try{ Class c = Class.forName("android.hardware.Camera"); Method spcwb = null; Method[] m = c.getMethods(); for (int i = 0; i < m.length; i++) { if (m[i].getName().compareTo("setAutoExposureLock") == 0) { spcwb = m[i]; break; } } if (spcwb != null) { Object[] arglist = new Object[1]; arglist[0] = tf; spcwb.invoke(cm, arglist); Log.i(TAG, "setAutoExposureLock: Called method"); } else { Log.i(TAG, "setAutoExposureLock: Did not find method"); } }catch(...){...} however, I tried this on both Nexus S (ICS 4.1) and Nexus 4 (ICS 4.2), the above code returned "not found". Shouldn't setAutoExposureLock be supported by 4.0.3 and newer? I directly called this method for my Nexus 4, it worked. But I'd like to make this work for older phones. I changed the class name to "android.hardware.Camera.Parameters", it did not help. did I miss anything? Qianqian -- -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[android-developers] Re: setAutoExposureLock method is not found in ICS
On Mar 6, 7:42 pm, Mark Murphy wrote: > Why not set your build target to API Level 14 and just call the > method, wrapping your calls in a Build.VERSION.SDK_INT check? Why are > you messing around with reflection games? hi Mark I copied the code template from some old code found online. I wasn't aware of Build.VERSION.SDK_INT. It is definitely the better way to go. thanks a lot Qianqian > > > > On Wed, Mar 6, 2013 at 7:21 PM, Qianqian Fang wrote: > > hi list > > > I am working on a camera program where I want to call setAutoExposureLock() > > is supported. To make this backward compatible, I used some sample code I > > found online to check the presence of this method first. The code snippet is > > below: > > > ... > > try{ > > Class c = Class.forName("android.hardware.Camera"); > > > Method spcwb = null; > > Method[] m = c.getMethods(); > > for (int i = 0; i < m.length; i++) { > > if (m[i].getName().compareTo("setAutoExposureLock") == 0) { > > spcwb = m[i]; > > break; > > } > > } > > if (spcwb != null) { > > Object[] arglist = new Object[1]; > > arglist[0] = tf; > > spcwb.invoke(cm, arglist); > > Log.i(TAG, "setAutoExposureLock: Called method"); > > } else { > > Log.i(TAG, "setAutoExposureLock: Did not find method"); > > } > > }catch(...){...} > > > however, I tried this on both Nexus S (ICS 4.1) and Nexus 4 (ICS 4.2), the > > above code returned "not found". Shouldn't setAutoExposureLock be supported > > by 4.0.3 and newer? I directly called this method for my Nexus 4, it worked. > > But I'd like to make this work for older phones. > > > I changed the class name to "android.hardware.Camera.Parameters", it did not > > help. > > > did I miss anything? > > > Qianqian > > > -- > > -- > > You received this message because you are subscribed to the Google > > Groups "Android Developers" group. > > To post to this group, send email to android-developers@googlegroups.com > > To unsubscribe from this group, send email to > > android-developers+unsubscr...@googlegroups.com > > For more options, visit this group at > >http://groups.google.com/group/android-developers?hl=en > > --- > > You received this message because you are subscribed to the Google Groups > > "Android Developers" group. > > To unsubscribe from this group and stop receiving emails from it, send an > > email to android-developers+unsubscr...@googlegroups.com. > > For more options, visithttps://groups.google.com/groups/opt_out. > > -- > Mark Murphy (a Commons > Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy > > _The Busy Coder's Guide to Android Development_ Version 4.6 Available! -- -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[android-developers] can not set preview buffer format to nv21 on nexus 4
Based on the help for getSupportedPreviewFormats [1], NV21 should be always supported. However, when I run camParam = mCamera.getParameters(); camParam.setPictureFormat(ImageFormat.NV21); mCamera.setParameters(camParam); on my nexus 4, I get an error "setParameters failed". The only way to get through is to use ImageFormat.JPEG, which is the default. is nv21 supported on nexus 4? if I can not set format to nv21, how can I read the RGB values per pixel with an JPEG preview buffer format? thanks Qianqian [1] http://developer.android.com/reference/android/hardware/Camera.Parameters.html#getSupportedPreviewFormats() -- -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[android-developers] Processing camera preview frames and display results via OpenGL
hi there I am trying to write a program to process camera preview data on-the- fly, and display the results as (semitransparent) overlays. I found one example that is particularly useful: http://nhenze.net/?p=172 the source code is at http://nhenze.net/uploads/OpenGL_Cam_1_2.zip In this sample, two Views are used, one SurfaceView for camera preview (CamLayer.java), one GLSurfaceView for showing the processed image (GLLayer.java). Unfortunately, the sample code maps the image to a rotating cube, while in my case, I just need to match the view with the camera preview in the background. I am new to OpenGL. I am wondering if there is a simple way to display a 2D texture on a GLSurfaceView that matches the preview SurfaceView? Appreciated if there are any suggestions. Qianqian -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Running camera preview while recording video at the same time
hi I am working on a real-time video processing project. I used setPreviewCallbackWithBuffer() and display both the preview image and the processed image on SurfaceViews. Now, I'd like to record video at the same time. I read from some posts that the video recording involves low-level io that is not in conflict with the preview buffer. I took some sample code from the second post of this link to do the video recording: http://stackoverflow.com/questions/1817742/android-video-recording-sample however, after merging it with my camera setPreviewCallbackWithBuffer() code, I kept getting "MediaRecorder: start called in an invalid state 4" when I tried to call mrec.start(). Both the initRecorder() and prepareRecorder() ran fine when the surface is created. Anyone know what might be causing this error? the MediaRecorder object does share the same camera object that is used for buffered preview. thanks Qianqian -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] saving camera preview frames to a file with minimum slow-down
hi list I'd like to get some advice on optimizing file IO of a camera-frame processing code I am working on. I used the setPreviewCallbackWithBuffer() method to capture each preview frame and do some processing in the real-time. The processed frame (an int array) is then save to the sdcard. I found that by commenting out the file io block, my frame rate can go up to 30fps for a 512x512 preview size. However, with the data saving, the frame rate is only around 10 fps or less. Any suggestions to speed up this? Qianqian -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Latency of onPreviewFrame callback when using setPreviewCallbackWithBuffer()
hi developers I am working on an android program to control an external hardware and process camera images in the real time. The hardware interface is an IOIO (http://www.sparkfun.com/products/10585). It controls an LED array to turn on and off. The software captures images from the camera using setPreviewCallbackWithBuffer() and store a few image frames per LED state. The camera thread and IOIO thread communicate by wait/notify. Unfortunately, I found that the saved camera frames are not synchronized with the IOIO pin status; I can only get synchronized images when I insert a Thread.sleep(delay>250) between toggling the IOIO pins and signaling camera to start saving data. At first, I suspected that this was caused by the communication latency on the IOIO side. But per IOIO's author, Ytai Ben-Tsvi, this is way too long compared to the expected latency (1~4ms), see http://groups.google.com/group/ioio-users/browse_thread/thread/f81f057bda73c525?hl=en If IOIO is ruled out, I now start questioning if there is a delay when the onPreviewFrame() is triggered. Is there a way to quantify the latency of the onPreviewFrame callbacks? The surfaceview seems to get updated very quickly, but I don't know how fast a displayed frame can be passed to onPreviewFrame() for processing. Any android documents discuss about this? Qianqian -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Latency of onPreviewFrame callback when using setPreviewCallbackWithBuffer()
I figure out something myself. I ran a crude experiment and my estimate to this delay is about 210 ms on my cell phone (Galaxy Epic) running Android 2.3.6. Here is how I did my experiment: 1. I first modified a code sample of Chronometer and wrote a program to display a 1/100-second accuracy-timer. I ran this timer on another android phone. 2. My camera code handles the onPreviewFrame and pass the buffer to a GLSurfaceView object and map it to a 2D texture. This texture is updated for each call of onPreviewFrame. 3. I pointed the camera phone to the phone running the timer, and focus. 4. I used a DSLR, set exposure time to 1/100 second, and took a few shots with the timer running A sample photo can be found here: http://nmr.mgh.harvard.edu/~fangq/temp/androidcamera/frame_latency_test.jpg >From 8 shots, I removed 2 that showed overlapping digits. In the remaining 6, the mean delay is 208.3 ms with std=4 ms. I understand that this latency includes the OpenGL texture mapping, however, I suspected that it is small compare to the frame delay (otherwise, please correct me). I will test this on other phones and see if this is consistent on the 2.3.x androids. Qianqian On Apr 3, 2:12 pm, Qianqian Fang wrote: > hi developers > > I am working on an android program to control an external hardware > and process camera images in the real time. The hardware interface > is an IOIO (http://www.sparkfun.com/products/10585). It controls an > LED array to turn on and off. The software captures images from the > camera > using setPreviewCallbackWithBuffer() and store a few image frames > per LED state. The camera thread and IOIO thread communicate > by wait/notify. > > Unfortunately, I found that the saved camera frames are not > synchronized > with the IOIO pin status; I can only get synchronized images when > I insert a Thread.sleep(delay>250) between toggling the IOIO pins > and signaling camera to start saving data. > > At first, I suspected that this was caused by the communication > latency on the IOIO side. But per IOIO's author, Ytai Ben-Tsvi, > this is way too long compared to the expected latency (1~4ms), see > > http://groups.google.com/group/ioio-users/browse_thread/thread/f81f05... > > If IOIO is ruled out, I now start questioning if there is > a delay when the onPreviewFrame() is triggered. > > Is there a way to quantify the latency of the onPreviewFrame > callbacks? > The surfaceview seems to get updated very quickly, but I don't know > how fast a displayed frame can be passed to onPreviewFrame() for > processing. > > Any android documents discuss about this? > > Qianqian -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] converting RGB565 byte array to integer buffer generate wrong results
hi I am working on an android program to process multiple frames of camera images. I used the setPreviewCallbackWithBuffer function and process the data inside the PreviewCallback. The code works generally ok, except the resulting data have weird values. I set the camera picture format to RGB565, and then create an int[] buffer, with the same size of the byte[] in a frame, and taking accumulated image of the red-channel. The following is my code: public class NIRSActivity extends Activity { int[] avg; // integer buffer to accumulate the red channel int frame; // frame counter public void onCreate(Bundle savedInstanceState) { avg=new int[320*480]; // do I need to do Array.fill? frame=0; param.setPreviewSize(320,480); camera.setPreviewCallbackWithBuffer(onPreviewFrame); camera.startPreview(); } public void onPreviewFrame(byte[] data, Camera camera) { // extract the red channel from RGB565 for(int i=0;i byte red=(byte) ((data[i] >> 3 ) & 0x1F); // RGGG | GGGB avg[i>>1]+=red; } ByteBuffer byteBuf = ByteBuffer.allocate(2 * data.length); //data.length is 2*320*480 IntBuffer intBuf = byteBuf.asIntBuffer(); intBuf.put(avg); FileOutputStream out = new FileOutputStream( "/sdcard/nirs_avg_"+Integer.toString(frame)+".raw"); out.write(byteBuf.array()); out.flush(); out.close(); frame++; } } if I plot the data, converted to a 2D image in matlab, the images look like wrapped phase maps, with large numbers around 10^8 ~ 10^9. I am not familiar with Java. Can someone please give me a hint which part of my code is wrong? I really appreciate your input. thank you in advance. Qianqian -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: converting RGB565 byte array to integer buffer generate wrong results
On Jul 11, 4:50 pm, Streets Of Boston wrote: > In the preview, the data is not RGB. It is yuv_420 or a similar format. > Query the camera-parameters for the exact format and decode the data > accordingly. hi thanks for your comment. In the "..." part of my original post, I did call "param.setPreviewFormat(PixelFormat.RGB_565)" to explicitly set the pixel format of the preview. I verified it by decoding the resulting byte buffer using an RGB_565-encoded bitmap "bmp=new Bitmap.createBitmap(320,480, Bitmap.Config.RGB_565)" and display the decoded bitmap to an imageview; the image looked ok. So, I am reasonably confident that the preview is RGB_565 encoded. Can anything else in my code mess up the decoding/recording? thanks Qianqian -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: converting RGB565 byte array to integer buffer generate wrong results
On Jul 11, 4:50 pm, Streets Of Boston wrote: > In the preview, the data is not RGB. It is yuv_420 or a similar format. > Query the camera-parameters for the exact format and decode the data > accordingly. problem found! it was caused by endianness of the data. By inserting byteBuf.order(ByteOrder.LITTLE_ENDIAN); before saving the buffer, the results became reasonable. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] strange camera preview image buffer wrapping problem
hi I found a strange problem when processing the preview images grabbed from my Epic 4G phone camera (android 2.2.1). The image data were captured in a callback set up by setPreviewCallbackWithBuffer(). The pixelformat of the image were set to RGB565. For each callback, I simply dump the raw data via FileOutputStream and then add the buffer back. The 1st to 6th image frames are shown in the following link (plotted with matlab, I did not bother to decode it). http://nmr.mgh.harvard.edu/~fangq/temp/camera_preview_image_wrapping.png From this plot, you can see the image wraps around the y-axis: for each frame, the y boundary moves about 4 pixels. My preview size was set to 480x320; the orientation was portrait. has anyone reported this issue before? anything that I should do to correct this? thanks Qianqian -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en