Code example for SurfaceView

Methods: getHoldersetVisibility

0
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        } 
        mCamera = openCamera(cameraId);
        SurfaceHolder holder = mSurfaceView.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
 
        Camera.Parameters p = mCamera.getParameters();
 
        if (cameraId == CAMERA_FACING_BACK) p.setRotation(270);
        p.setPreviewFormat(ImageFormat.NV21);
        Size lowestSetting = p.getSupportedPreviewSizes().get(1);
        p.setPreviewSize(lowestSetting.width, lowestSetting.height);
         
        mCamera.setParameters(p);
        p = mCamera.getParameters();
        mPreviewSize = p.getPreviewSize();
        textureSize = Utilities.nextPowerOfTwo(Math.max(mPreviewSize.width, mPreviewSize.height));
        mTextureRatio = new TextureRatio(((float)mPreviewSize.width) / (float)textureSize, 1.0f - (float)mPreviewSize.height / (float)textureSize);
 
        cameraTexture = null;
        initialized = false;
        finishCameraInit(); 
    } 
 
    public TextureRatio getTextureRatio() { 
        return mTextureRatio;
    } 
 
    public void onPreviewFrame(byte[] yuvs, Camera camera) {
        byte[] rgb = useOtherBuffer ? rgbBytes2 : rgbBytes;
        GLLayer.decode(yuvs, mPreviewSize.width, mPreviewSize.height, textureSize, rgb);
        if (mCamera != null) mCamera.addCallbackBuffer(yuvs);
        mDirty = true;
        useOtherBuffer = !useOtherBuffer;
    } 
     
    public void bindTexture() { 
        synchronized (this) {
            if (cameraTexture == null)  {
                cameraTexture = new int[1];
                GLES20.glGenTextures(1, cameraTexture, 0);
            } 
            int tex = cameraTexture[0];
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex);
            if (cameraBytes.length > 0) {
                if (!initialized) {
                    // todo: i wish there was a better way to reliably clear out a texture. 
                    Bitmap b = Bitmap.createBitmap(textureSize, textureSize, Bitmap.Config.RGB_565);
                    b.eraseColor(0);
                    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, b, 0);
                    b.recycle();
                    initialized = true;
                } 
                if (mDirty) {
                    ByteBuffer buff = useOtherBuffer ? rgbBuffer : rgbBuffer2;
                    GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, mPreviewSize.width, mPreviewSize.height,
                            GLES20.GL_RGB, GLES20.GL_UNSIGNED_BYTE, buff);
                    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
                    mDirty = false;
                } 
            } 
        } 
    } 
     
    public void onPause() { 
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    } 
     
    public void onResume() { 
        if (mCamera == null)
            initCamera(mCameraId);
    } 
 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        finishCameraInit(); 
    } 
     
    private void finishCameraInit() { 
        mPreviewSize = mCamera.getParameters().getPreviewSize();
        textureSize = Utilities.nextPowerOfTwo(Math.max(mPreviewSize.width, mPreviewSize.height));
        mTextureRatio = new TextureRatio(((float)mPreviewSize.width) / (float)textureSize, 1.0f - (float)mPreviewSize.height / (float)textureSize);
        int bytelen = (int)(mPreviewSize.width * mPreviewSize.height * ImageFormat.getBitsPerPixel(ImageFormat.NV21) / 8.0f);
        cameraBytes = new byte[bytelen];
        rgbBytes = new byte[mPreviewSize.width * mPreviewSize.height * 3];
        rgbBuffer = ByteBuffer.wrap(rgbBytes);
        rgbBytes2 = new byte[mPreviewSize.width * mPreviewSize.height * 3];
        rgbBuffer2 = ByteBuffer.wrap(rgbBytes2);
         
        mCamera.addCallbackBuffer(cameraBytes);
        mCamera.setPreviewCallbackWithBuffer(this);
         
        mCamera.startPreview();
         
        mSurfaceView.setVisibility(View.GONE);
         
        System.out.println("camz CameraPreviewSink startPreview");
    } 
 
    public void surfaceCreated(SurfaceHolder holder) {