To use setPreviewCallbackWithBuffer problem

All your problems with Audio, Video and Images.

To use setPreviewCallbackWithBuffer problem

Postby mlianghua » Tue May 17, 2011 1:57 pm

when I use setPreviewCallbackWithBuffer to impove cpu efficent, there is a problem which takepicture can't take affect.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.   @Override
  2.         protected void onCreate(Bundle savedInstanceState) {
  3.         super.onCreate(savedInstanceState);
  4.        
  5.         // Hide the window title.
  6.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  7.    
  8.         // Create our Preview view and set it as the content of our activity.
  9.         mPreview = new Preview(this);
  10.         setContentView(mPreview);
  11.        
  12.         mPreview.setOnClickListener(new View.OnClickListener()
  13.         {                      
  14.                         @Override
  15.                         public void onClick(View v)
  16.                         {
  17.                                 // TODO Auto-generated method stub
  18.                                 mPreview.mCamera.takePicture(null, null, mPreview);
  19.                                 mPreview.mCamera.startPreview();
  20.                         }
  21.                 });
  22.     }
  23.  
  24. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
  1. [syntax="java"]class Preview extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback, PictureCallback 
  2. SurfaceHolder mHolder; 
  3. Camera mCamera; 
  4.  
  5. Preview(Context context) { 
  6. super(context); 
  7.  
  8. // Install a SurfaceHolder.Callback so we get notified when the 
  9. // underlying surface is created and destroyed. 
  10. mHolder = getHolder(); 
  11. mHolder.addCallback(this); 
  12. mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
  13.  
  14. public void surfaceCreated(SurfaceHolder holder) { 
  15. // The Surface has been created, acquire the camera and tell it where 
  16. // to draw. 
  17. mCamera = Camera.open(); 
  18. try { 
  19. mCamera.setPreviewDisplay(holder); 
  20. } catch (IOException exception) { 
  21. mCamera.release(); 
  22. mCamera = null; 
  23. // TODO: add more exception handling logic here 
  24.  
  25.  
  26. public void surfaceDestroyed(SurfaceHolder holder) { 
  27. // Surface will be destroyed when we return, so stop the preview. 
  28. // Because the CameraDevice object is not a shared resource, it's very 
  29. // important to release it when the activity is paused. 
  30. mCamera.stopPreview(); 
  31. mCamera.release(); 
  32. mCamera = null; 
  33.  
  34. public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
  35. // Now that the size is known, set up the camera parameters and begin 
  36. // the preview. 
  37.  
  38. w=800; //I am a very bad person ... hard coding! 
  39. h=480; //I am a very bad person ... hard coding! 
  40.  
  41. Camera.Parameters parameters = mCamera.getParameters(); 
  42. parameters.setPreviewSize(w, h);  
  43. mCamera.setParameters(parameters); 
  44. PixelFormat p = new PixelFormat(); 
  45. PixelFormat.getPixelFormatInfo(parameters.getPreviewFormat(),p); 
  46. int bufSize = (w*h*p.bitsPerPixel)/8; 
  47.  
  48. //Must call this before calling addCallbackBuffer to get all the 
  49. // reflection variables setup 
  50. initForACB(); 
  51.  
  52. //Add three buffers to the buffer queue. I re-queue them once they are used in 
  53. // onPreviewFrame, so we should not need many of them. 
  54. byte[] buffer = new byte[bufSize]; 
  55. addCallbackBuffer(buffer);  
  56. buffer = new byte[bufSize]; 
  57. addCallbackBuffer(buffer); 
  58. buffer = new byte[bufSize]; 
  59. addCallbackBuffer(buffer); 
  60.  
  61. setPreviewCallbackWithBuffer(); 
  62. mCamera.startPreview(); 
  63. }  
  64.  
  65. /** 
  66. * This method will list all methods of the android.hardware.Camera class, 
  67. * even the hidden ones. With the information it provides, you can use the same 
  68. * approach I took below to expose methods that were written but hidden in eclair 
  69. */ 
  70. private void listAllCameraMethods(){ 
  71. try { 
  72. Class c = Class.forName("android.hardware.Camera"); 
  73. Method[] m = c.getMethods(); 
  74. for(int i=0; i<m.length; i++){ 
  75. Log.i("AR"," method:"+m[i].toString()); 
  76. } catch (Exception e) { 
  77. // TODO Auto-generated catch block 
  78. Log.i("AR",e.toString()); 
  79.  
  80. /** 
  81. * These variables are re-used over and over by 
  82. * addCallbackBuffer 
  83. */ 
  84. Method mAcb; 
  85. Object[] mArglist; 
  86.  
  87. private void initForACB(){ 
  88. try { 
  89. Class mC = Class.forName("android.hardware.Camera"); 
  90.  
  91. Class[] mPartypes = new Class[1]; 
  92. mPartypes[0] = (new byte[1]).getClass(); //There is probably a better way to do this. 
  93. mAcb = mC.getMethod("addCallbackBuffer", mPartypes); 
  94.  
  95. mArglist = new Object[1]; 
  96. } catch (Exception e) { 
  97. Log.e("AR","Problem setting up for addCallbackBuffer: " + e.toString()); 
  98.  
  99. /** 
  100. * This method allows you to add a byte buffer to the queue of buffers to be used by preview. 
  101. * See: http://android.git.kernel.org/?p=platfo ... 327e536ce1 
  102. *  
  103. * @param b The buffer to register. Size should be width * height * bitsPerPixel / 8. 
  104. */ 
  105. private void addCallbackBuffer(byte[] b) 
  106. //Check to be sure initForACB has been called to setup 
  107. // mAcb and mArglist 
  108. if(mArglist == null) 
  109. initForACB(); 
  110.  
  111. mArglist[0] = b; 
  112. try  
  113. mAcb.invoke(mCamera, mArglist); 
  114. }  
  115. catch (Exception e)  
  116. Log.e("AR","invoking addCallbackBuffer failed: " + e.toString()); 
  117.  
  118. /** 
  119. * Use this method instead of setPreviewCallback if you want to use manually allocated 
  120. * buffers. Assumes that "this" implements Camera.PreviewCallback 
  121. */ 
  122. private void setPreviewCallbackWithBuffer() 
  123. try  
  124. Class c = Class.forName("android.hardware.Camera"); 
  125.  
  126. Method spcwb = null; 
  127. //This way of finding our method is a bit inefficient, but I am a reflection novice, 
  128. // and didn't want to waste the time figuring out the right way to do it. 
  129. // since this method is only called once, this should not cause performance issues 
  130. Method[] m = c.getMethods(); 
  131. for(int i=0; i<m.length; i++) 
  132. if(m[i].getName().compareTo("setPreviewCallbackWithBuffer") == 0) 
  133. spcwb = m[i]; 
  134. break; 
  135.  
  136. //If we were able to find the setPreviewCallbackWithBuffer method of Camera,  
  137. // we can now invoke it on our Camera instance, setting 'this' to be the 
  138. // callback handler 
  139. if(spcwb != null) 
  140. Object[] arglist = new Object[1]; 
  141. arglist[0] = this; 
  142. spcwb.invoke(mCamera, arglist); 
  143. Log.i("AR","setPreviewCallbackWithBuffer: Called method"); 
  144. }  
  145. else  
  146. Log.i("AR","setPreviewCallbackWithBuffer: Did not find method"); 
  147.  
  148. } catch (Exception e) { 
  149. // TODO Auto-generated catch block 
  150. Log.i("AR",e.toString()); 
  151.  
  152. Date start; 
  153. int fcount = 0; 
  154. /** 
  155. * Demonstration of how to use onPreviewFrame. In this case I'm not 
  156. * processing the data, I'm just adding the buffer back to the buffer 
  157. * queue for re-use 
  158. */ 
  159. public void onPreviewFrame(byte[] data, Camera camera)  
  160. // TODO Auto-generated method stub 
  161. if(start == null){ 
  162. start = new Date(); 
  163. fcount++; 
  164. if(fcount % 30 == 0){ 
  165. double ms = (new Date()).getTime() - start.getTime(); 
  166. Log.i("AR","fps:" + fcount/(ms/1000.0)); 
  167.  
  168. //We are done with this buffer, so add it back to the pool 
  169. addCallbackBuffer(data); 
  170.  
  171. @Override 
  172. public void onPictureTaken(byte[] data, Camera camera)  
  173. // TODO Auto-generated method stub 
  174. Log.e("AR", "data: " + data); 
  175.  
  176. }[/syntax] 


But When I want to click mPreview to takepicture, the Application is halt and onPictureTaken is not called!
what's the reason?
mlianghua
Freshman
Freshman
 
Posts: 2
Joined: Sun Sep 19, 2010 3:48 am

Top

Re: To use setPreviewCallbackWithBuffer problem

Postby mlianghua » Wed May 25, 2011 2:48 am

ok! I find a resolution. we should addCallbackBuffer method in previewcallback and picturecallback!
mlianghua
Freshman
Freshman
 
Posts: 2
Joined: Sun Sep 19, 2010 3:48 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 5 guests