android相机预览黑色

时间:2014-06-13 06:17:30

标签: android camera preview

我的项目是一个相机应用程序,它具有在相机预览中添加视图的功能, 它通常有效。

但是在某些智能手机中,当视图添加时,预览将变为黑色,没有任何异常,当我粘贴图片时,图片是工作,而不是黑色。 所以我认为相机对象正在工作,而不是空或错误 我尝试更改预览尺寸,我从parameters.getSupportedPreviewSizes()获得预览尺寸。 我尝试将1XXX * 800~144 * 108设置为预览尺寸,但都得到了相同的结果......

我该如何解决这个问题? 希望有更好的回应。谢谢

void cameraInit() {
    try {
        bGalleryOpen = false;
        mDATA.bFinish = true;
        Log.d("cameraInit", "Start");

        if (bmPic != null) {
            objPic.picRecycled(bmPic);
            System.gc();
        }

        setContentView(R.layout.activity_camera);
        RelativeLayout cmain = (RelativeLayout) findViewById(R.id.camera_main);

        ibShot = (ImageButton) findViewById(R.id.iBShot);
        ibShare = (ImageButton) findViewById(R.id.iBShare);
        ibAdd = (ImageButton) findViewById(R.id.iBAdd);
        ibGarelly = (ImageButton) findViewById(R.id.iBGarelly);
        ibSave = (ImageButton) findViewById(R.id.iBSave);
        ibBack = (ImageButton) findViewById(R.id.iBBack);

        ibShot.setVisibility(View.VISIBLE);
        ibShare.setVisibility(View.GONE);
        ibAdd.setVisibility(View.VISIBLE);
        ibGarelly.setVisibility(View.VISIBLE);
        ibSave.setVisibility(View.GONE);
        ibBack.setVisibility(View.VISIBLE);

        ibAdd.setOnClickListener(oclAdd);
        ibAdd.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (event.getAction() == MotionEvent.ACTION_DOWN) {

                    ibAdd.setImageResource(R.drawable.cra_007_over);

                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    ibAdd.setImageResource(R.drawable.cra_007);
                }
                return false;
            }
        });
        ibShot.setOnClickListener(oclCameraClick);
        ibGarelly.setOnClickListener(oclGallery);
        ibBack.setOnClickListener(oclBack);

        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        mCamera = openCamera();
        if (mCamera != null) {              
            mPreview = new MyPreview(this, mCamera);
            FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);

            RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) preview
                    .getLayoutParams();
            linearParams.height = dm.heightPixels;
            linearParams.width = dm.heightPixels / 3 * 4;
            Log.d("Preview ", "weight:" + linearParams.width + "heigh:"
                    + linearParams.height);

            linearParams.addRule(RelativeLayout.LEFT_OF, R.id.lTool);
            preview.setLayoutParams(linearParams);

            preview.addView(mPreview);
        }

        //Add image view
        Bundle bGet = getIntent().getExtras();
        if (bGet != null) {
            strPicID = bGet.getString("imageID");
            if (strPicID != null) {

                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);

                if (dm.heightPixels != 0) {
                    lp = new RelativeLayout.LayoutParams(
                            dm.heightPixels / 3 * 4, dm.heightPixels);
                }

                lp.addRule(RelativeLayout.LEFT_OF, R.id.lTool);
                iDrawble = this.getResources().getIdentifier(strPicID,
                        "drawable", this.getPackageName());

                Options bo = new Options();
                bo.inJustDecodeBounds = true;
                bo.inSampleSize = 1;
                bo.inJustDecodeBounds = false;
                InputStream inputStream = this.getResources()
                        .openRawResource(iDrawble);
                Bitmap bm = BitmapFactory.decodeStream(inputStream, null,
                        bo);
                myView = new PictureView(this, bm);
                mDATA.alPicView.clear();
                mDATA.alPicView.add(myView);

                for (int i = 0; i < mDATA.alPicView.size(); i++) {
                    Log.d(TAG,
                            "All " + String.valueOf(mDATA.alPicView.size())
                                    + " View");
                    cmain.addView(mDATA.alPicView.get(i), lp);
                    Log.d(TAG, "Add " + String.valueOf(i + 1) + " View");
                }
                mDATA.bFinish = false;
                getIntent().removeExtra("imageSrc");
                getIntent().removeExtra("imageID");
            }
        }

    } catch (Exception errMsg) {
        errMsg.printStackTrace();
    }
}

和mypreview代码

public class MyPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
String TAG = "myPreview";
Size sPic;

public MyPreview(Context context, Camera camera) {
    super(context);
    mCamera = camera;

    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    Log.d(TAG, "CameraPreview");
}

public void surfaceCreated(SurfaceHolder holder) {

    try {
        Log.d(TAG, "CameraPreview:surfaceCreated");
        if (mCamera != null) {
            mCamera.setPreviewCallback(new PreviewCallback() {
                public void onPreviewFrame(byte[] data, Camera camera) {
                    Log.d(TAG, "onPreviewFrame: preview: data=" + data);
                }
            });
            mCamera.setOneShotPreviewCallback(new PreviewCallback() {
                public void onPreviewFrame(byte[] data, Camera camera) {
                    Log.d(TAG, "onPreviewFrame: short preview: data="
                            + data);
                }
            });
            mCamera.setErrorCallback(new ErrorCallback() {
                public void onError(int error, Camera camera) {
                    Log.d(TAG, "onError: error=" + error);
                }
            });
        }
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
    } catch (IOException e) {
        Log.d("surfaceCreated",
                "Error setting camera preview: " + e.getMessage());
        mCamera.release();
        mCamera = null;
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // empty. Take care of releasing the Camera preview in your
    // activity.

    Log.d(TAG, "CameraPreview:surfaceDestroyed");
    this.getHolder().removeCallback(this);
    if (mCamera != null) {
        mCamera.stopPreview();
        mCamera.release();
    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    Log.d(TAG, "CameraPreview:surfaceChanged W:" + w + ",H:" + h);

    if (mHolder.getSurface() == null) {
        Log.d(TAG, "CameraPreview:getSurface Null");
        // preview surface does not exist
        return;
    }

    // stop preview before making changes
    try {
        Log.d(TAG, "CameraPreview:surfaceChanged Stop");
        mCamera.stopPreview();
    } catch (Exception e) {
        // ignore: tried to stop a non-existent preview
        e.printStackTrace();
    }

    // start preview with new settings
    try {
        // set preview size and make any resize, rotate or          
        Camera.Parameters params = mCamera.getParameters();
        Camera.Size size = getBestPreviewSize(params, w, h);
        if (size != null) {
            Log.d(TAG, "CameraPreview:surfaceChanged get preSize:"
                    + size.width + "," + size.height);
            Size sPic = getBestPhotoSize(params, size.width, size.height);

            if (size != null) {
                mDATA.m_Size = size;                    
                params.setPreviewSize(size.width, size.height);                 
                params.setPictureSize(sPic.width, sPic.height);
            }
            Log.d(TAG,
                    "CameraPreview:surfaceChanged preSize:"
                            + params.getPreviewSize().width + ","
                            + params.getPreviewSize().height);
            Log.d(TAG,
                    "CameraPreview:surfaceChanged picSize:"
                            + params.getPictureSize().width + ","
                            + params.getPictureSize().height);

            mCamera.setParameters(params);
        }
        mCamera.startPreview();
        Log.d(TAG, "CameraPreview:surfaceChanged Start");
    } catch (Exception e) {
        // Log.d(TAG, "Error starting camera preview: " +
        e.printStackTrace();
        mCamera.release();
        mCamera = null;
    }
}

public Camera getCamera() {
    return mCamera;
}

public Camera.Size getBestPreviewSize(Camera.Parameters parameters, int w,
        int h) {
    Camera.Size result = null;
    float fRatio = 3 / 4;
    Log.d(TAG, "CameraPreview:getBestPreviewSize");
    try {
        for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
            Log.d("getBestPreviewSize", "suport w:" + size.width + "h:"
                    + size.height);
            if (size.width <= w && size.height <= h && (h / w) == fRatio) {
                if (null == result)
                    result = size;
                else {

                    int resultDelta = w - result.width + h - result.height;
                    int newDelta = w - size.width + h - size.height;

                    if (newDelta < resultDelta)
                        result = size;

                }
            }
        }
    } catch (Exception errMsg) {
        errMsg.printStackTrace();
    }           
    return result;
}

public Size getBestPhotoSize(Parameters parameters, int w, int h) {
    Size result = null;
    float fRatio = h / w;
    Log.d(TAG, "CameraPreview:getBestPreviewSize");
    try {
        for (Camera.Size size : parameters.getSupportedPictureSizes()) {
            Log.d("getBestPhotoSize", "suport w:" + size.width + "h:"
                    + size.height);
            float fTmp = size.height / size.width;
            if (fTmp == fRatio && size.width <= 1800) {
                if (null == result)
                    result = size;
                else {

                    int resultDelta = w - result.width + h - result.height;
                    int newDelta = w - size.width + h - size.height;

                    if (newDelta < resultDelta)
                        result = size;                      
                }
            }
        }
    } catch (Exception errMsg) {
        errMsg.printStackTrace();
    }       
    return result;
}

}

1 个答案:

答案 0 :(得分:0)

我有类似的问题。我的View大小设置为0导致我的,因此从未调用surfaceCreated回调。

相关问题