Android相机预览未显示

时间:2016-09-17 16:16:27

标签: java android xml android-fragments android-camera

我在线here找到了一个用于捕捉图像的教程,它们非常相似,我互相用来弄清楚我的相机代码无法正常工作的原因。

我在Android中没有得到任何语法错误,但是当我继续使用所需的片段时,它只是一个白色的屏幕,没有相机显示器,我不知道为什么我已经深入查看了两个代码示例并用Google搜索了我的问题,但无法找到任何东西。与我的代码唯一的区别是它是一个片段而不是一个活动。有人可以帮助我吗?

这是我的代码:

public class Image extends Fragment implements SurfaceHolder.Callback {

private ImageView imageView;
private SurfaceView mSurfaceView;
private Bitmap capturedImage;


//Camera

private SurfaceHolder sHolder;
private Camera mCamera;
private Parameters parameters;

/**********************************************/

public Image() {
    // Required empty public constructor
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.image_activity, container, false);

    imageView = (ImageView) view.findViewById(R.id.imageView);
    mSurfaceView = (SurfaceView) view.findViewById(R.id.surfaceView);

    //Get a surface
    sHolder = mSurfaceView.getHolder();

    //add the callback interface methods defined below as the Surface View callbacks
    sHolder.addCallback(this);

    //tells Android that this surface will have its data constantly replaced
    sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    return view;
}

@Override
public void surfaceCreated(SurfaceHolder holder) {

    // The Surface has been created, acquire the camera and tell it where
    // to draw the preview.

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

    } catch (IOException exception) {
        mCamera.release();
        mCamera = null;
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {


    //get camera parameters
    parameters = mCamera.getParameters();

    parameters.setPreviewSize(352, 288);
    //set camera parameters
    mCamera.setParameters(parameters);
    mCamera.startPreview();

    //sets what code should be executed after the picture is taken
    Camera.PictureCallback mCall = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            //decode the data obtained by the camera into a Bitmap
            capturedImage = BitmapFactory.decodeByteArray(data, 0, data.length);
            String filename= Environment.getExternalStorageDirectory()
                    + File.separator + "testimage.jpg";
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(filename);
                capturedImage.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
                // PNG is a lossless format, the compression factor (100) is ignored
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (out != null) {
                        out.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //set the iv_image
            imageView.setImageBitmap(capturedImage);
        }
    };

    mCamera.takePicture(null, null, mCall);

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

    mCamera.stopPreview();
    mCamera.release();
    mCamera = null;
  }
}

这是我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<SurfaceView
    android:id="@+id/surfaceView"
    android:layout_height="0dip"
    android:layout_width="0dip">

</SurfaceView>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView">

</ImageView>

</LinearLayout>

更新1:

这是我的清单文件,我忘了包含这个:

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

我还在应用程序的marshmallow设置中启用了权限,但仍然没有显示任何内容

更新2: 刚尝试使用API​​ 17设备,仍然没有预览

3 个答案:

答案 0 :(得分:1)

请确保您已将必要的相机权限添加到AndroidManifest.xml文件&amp;如果您使用的是棉花糖,请再次检查设置=&gt;启用权限的步骤应用=&GT;应用程序管理器=&gt;你的App =&gt;权限

答案 1 :(得分:1)

假设您的所有代码实际上都按预期运行,可能有一种可能性:setPreviewSize(352, 288) - 可能不支持该大小。

您需要查看支持的预览尺寸列表并选择一个,或者使用基本上始终可用的320,240或640,480。

答案 2 :(得分:0)

XML中的

android_height和android_width为0,将其更改!