简体   繁体   English

Android相机预览黑

[英]android camera preview black

My project is a camera app it has a function for add a view in camera preview, it usually works. 我的项目是一个相机应用程序,它具有在相机预览中添加视图的功能,通常可以正常工作。

But in some smartphone when the view add, preview will turn black without any exception and when I tack picture , the picture is work, not black. 但是在某些智能手机中,当添加视图时,预览将毫无例外地变成黑色,而当我添加图片时,图片有效,而不是黑色。 So I think camera object is working, not null or error I have try to change preview size,i get preview size from parameters.getSupportedPreviewSizes() . 所以我认为相机对象正在工作,不是null或错误,我尝试更改预览大小,我从parameters.getSupportedPreviewSizes()获取预览大小。 I try setup 1XXX*800 ~ 144*108 into preview size, but all got the same result... 我尝试将1XXX * 800〜144 * 108设置为预览尺寸,但结果都相同...

How could I solve the issue? 我该如何解决这个问题? Hoping for better response. 希望能有更好的回应。 Thanks 谢谢

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

and mypreview code 和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;
}

} }

I had a similar problem. 我有一个类似的问题。 Mine was caused by the size of my View being set to 0, and consequently the surfaceCreated callback was never called. 我的是由于我的View的大小设置为0 surfaceCreated ,因此从未调用surfaceCreated回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM