1

我正在onPreviewFrame(byte[] data, Camera camera)Android上工作。在 onPreviewFrame 中我做了一些图像处理。在 onPreviewFrame 内的某个点,我想停止预览(我通过 if 语句知道这一点)并播放声音 - 也许是电话铃声。我认为您无法在预览中播放声音。

如何退出 onPreviewFrame 以及在哪里添加播放声音的代码?

是在 Surface Destroyed 上吗?

这是我的代码:

public class MyCameraPreview extends Activity {    
private Preview mPreview;
     public TextView results;



    @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, PreviewCallback {   

    SurfaceHolder mHolder;   
    Camera mCamera;   
    public TextView results;
    public TextView txt;
    private Parameters parameters;   
    //this variable stores the camera preview size    
    private Size previewSize;   
    //this array stores the pixels as hexadecimal pairs    
    private int[] pixels;   
    public int[] argb8888;

    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);   

           //sets the camera callback to be the one defined in this class   
           mCamera.setPreviewCallback(this);

           parameters = mCamera.getParameters();   
           parameters.setZoom(parameters.getMaxZoom());
           mCamera.setParameters(parameters);
           parameters = mCamera.getParameters();
           previewSize = parameters.getPreviewSize();   
           pixels = new int[previewSize.width * previewSize.height]; 


        } catch (IOException exception) {   
            mCamera.release();   
            mCamera = null;   
            // TODO: add more exception handling logic here   
        }   
    }   

    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.   
        parameters.setPreviewSize(w, h);   
        //set the camera's settings   
        mCamera.setParameters(parameters);   
        mCamera.startPreview();


    }   

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


Do some image processing.


If condition == true {

Exit the preview and then play the ringtone and exit the application.

}   

    }
4

1 回答 1

1

在预览中,您可以添加侦听器的定义并使用它来不在预览中添加声音。

像这样的东西:

public static interface OnPreviewListener {
    void onImageMakeSound();
}

public void setListener(OnPreviewListener listener) {
        this.listener = listener;
}

然后在您的 onPreviewFrame 方法中:

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
    if (countFrame  > 5) { 
        imageBytes = data;
        countFrame = 0;
        if (listener != null)
            listener.onImageMakeSound();
    }
    countFrame++;
    camera.addCallbackBuffer(data);
    return;
}

在您的活动中,必须实现OnPreviewListener

 @Override
    public void onImageMakeSound() {
        alarmSoundOn();

    }
于 2013-02-13T15:56:03.747 回答