开发者

onPreviewFrame doesn't change the data

It is my first time programming on Android, and more I am not that familiar with the java programmation. I tried to find some answer on my problem over the internet but didn't find any solution. I hope one of you will be able to help me. What I want to do is applying some image processing in real time to the video camera and display it. That is why I am using the function onPreviewFrame the make some image processing before display the result. Please tell me if it isn't the best way. my problem is that when I try to modify the data array, nothing as changed on the screen. From what I saw with the debugger the function is called. All of that is done with the 2.1 API.

Does someone know why? How can i solve this?

To help, here is my code :

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import java.io.IOException;

// ----------------------------------------------------------------------

public class CameraPreview extends Activity 开发者_如何转开发{    
    private Preview mPreview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // Create our Preview view and set it as the content of our activity.
        mPreview = new Preview(this);
        setContentView(mPreview);
    }

}

// ----------------------------------------------------------------------

class Preview extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback {
    SurfaceHolder mHolder;
    Camera mCamera;

    Preview(Context context) {
        super(context);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.

        mCamera = Camera.open();
        try {
            mCamera.setPreviewDisplay(holder);      
            mCamera.setPreviewCallback(this);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }


    public void onPreviewFrame(byte[] data, Camera camera) { 
        if (data != null) {
            for(int i=0;i<data.length;i++){
                data[i] = 0;
            }
        }

    }


    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }

}

Thank you for the help, Morgan Cormier


Hey there! The buffer (byte[] data) that is passed into onPreviewFrame is a copy of the camera frame buffer, so any modifications to it wont be reflected when the camera preview is drawn.

I've only just started Android dev myself, but AFAIK the best option is to take that buffer and draw it to another SurfaceView, e.g. to an openGL surface. I've grabbed a sample that should demonstrate this:

http://nhenze.net/?p=154

Following that example, you can take your image data, perform some processing then render it. You could always dabble with pixel shaders in GL to perform your image processing instead, if you like (AFAIK - I haven't attempted this myself on Android).

Hope that helps!


For me only works if I install the PreviewCallback in the method on SurfaceChanged

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜