简体   繁体   English

Android Camera预览未显示

[英]Android Camera preview not showing

I found a tutorial online here both for capturing images, they are very similar and I used one another to figure out why my Camera code isn't working. 我在这里找到了一个在线教程,用于捕获图像,它们非常相似,并且我互相探讨了为什么我的Camera代码无法正常工作。

I do not get any syntax errors in Android but when I go on the desired fragment it is just a white screen, there is no camera display and I have no idea why I have looked at both code examples in depth and googled my problem but cant find anything. 我在Android中没有收到任何语法错误,但是当我进入所需的片段时,它只是一个白屏,没有相机显示,我也不知道为什么我深入研究了两个代码示例并用谷歌搜索了我的问题,但是无法找到任何东西。 The only difference with my code is that its a fragment instead of an activity. 我的代码的唯一区别是它是片段而不是活动。 Can someone please help me? 有人可以帮帮我吗?

Here is my code: 这是我的代码:

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

Here is my XML file: 这是我的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>

Update 1: 更新1:

Here is my manifest file, I forgot to include this: 这是我的清单文件,我忘了包含此文件:

<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" />

I have also enabled the permissions in marshmallow settings for the application but still doesn't show anything 我还为应用程序启用了棉花糖设置中的权限,但仍然不显示任何内容

Update 2: Just tried it with a API 17 device and there is still no preview 更新2:刚刚尝试使用API​​ 17设备,但仍然没有预览

请确保已将必需的相机权限添加到AndroidManifest.xml文件中;如果您使用的是蛋白软糖,请再检查一步,从设置=>应用程序=>应用程序管理器=>您的应用程序=>权限中启用权限

Assuming all your code is actually running as you expect, one possibility: setPreviewSize(352, 288) - that size may not be supported. 假定所有代码实际上都按预期运行,一种可能性是: setPreviewSize(352, 288) -可能不支持该大小。

You'll need to check the list of supported preview sizes and pick one, or use 320,240 or 640,480 which are basically always available. 您需要检查受支持的预览尺寸列表,然后选择一个,或者使用基本上总是可用的320,240或640,480。

在XML中,android_height和android_width为0,将其更改!

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

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