Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

really confused with setPreviewCallback in Android, need advice

I'm building an application on Android to take frames from the camera, process them, and then display the frame on a surfaceView, as well as drawing on the SurfaceView via the canvas and drawbitmap and all.

Just to check, is SurfaceView and Bitmaps and Canvases the best way to do it ? I'm after speed.

Assuming the answer to the above is Yes, the question would be: Where should I place the following function

camera_object.setPreviewCallback(new PreviewCallback()
     public void onPreviewFrame(byte[] data, Camera camera){

should I place it in onCreate() or should I place it in surfaceCreated() or surfaceChanged() ?

I declared my mainactivity class as follows:

public class MainActivity extends Activity implements SurfaceHolder.Callback, Camera.PreviewCallback
{

and in that class Eclipse forces me to create an override function for onpreviewframe in the MainActivity class as follows

public void onPreviewFrame(byte[] data, Camera camera){
}

but it never gets called. Should I try to use this function ? is it better to use it ? or is it just an Eclipse thing ?

Please advise

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Are you calling setPreviewDisplay(), startPreview() and setPreviewCallback(this) from the app? Without that you will not get any calls to onPreviewFrame(). In fact if you are using SurfaceView, then the callback preview buffers are a copy of the actual buffers that are being displayed on the screen. So if you want to display these copied buffers, you need to create a new view and overwrite it. This would be inefficient. I would suggest you use SurfaceTexture instead and use 'onFrameAvailable' callback to get the frames and then draw & display manually. An example of this can be found in the PanoramaActivity code of the default Android Camera App.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...