2016-09-17 114 views
0

我找到了一个教程在线here这两个捕获图像,他们非常相似,我互相找出为什么我的相机代码不工作。Android相机预览不显示

我在Android中没有得到任何语法错误,但是当我在所需的片段上时,它只是一个白色屏幕,没有摄像头显示,我不知道为什么我已经深入查看了这两个代码示例,我的问题,但无法找到任何东西。我的代码唯一的区别是它的一个片段而不是一个活动。有人可以帮帮我吗?

这里是我的代码:

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

我也启用在应用程序棉花糖设置权限,但依然什么都不显示

更新2: 用API 17设备就尝试过了,仍然没有预览

+0

任何人都可以帮忙吗?我试过了我所知道的一切 – AndroidLearner

+0

我从字面上复制并将这些代码粘贴到一个新类中,也扩展了活动,仅用于测试,仍然不起作用。有人可以测试这个请求吗? http://stackoverflow.com/documentation/android/4789/camera-and-gallery/12655/take-photo#t=201609161413543951207 – AndroidLearner

+0

一些基本的调试技巧: 1)在启动的每个阶段添加一些日志语句以确保它们都按您期望的顺序运行。如果没有,那么就有线索。 2)包括启动你的应用程序的logcat输出与问题 –

回答

1

请确保您已经添加了必要的相机允许进入AndroidManifest.xml文件&如果您使用的棉花糖请检查许可从设置=>应用程序=>应用程序管理器=>您的应用程序=>权限

+0

我更新了我的文章 – AndroidLearner

1

假设启用一个步骤所有代码都已经实际运行为你预计,一种可能性:setPreviewSize(352, 288) - 可能不支持该大小。

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