surfaceView错误中的相机预览

时间:2018-06-08 22:40:44

标签: android

以下代码旨在在surfaceView中进行相机预览,KameraSurface类是我拥有生成相机预览会话的代码的地方,并且在"公共视图onCreateView"有一个KameraSurface类的初始化,它应该在surfaceView中生成相机预览。问题是,当我在手机上运行此代码时,我收到错误,应用程序将关闭。

您认为问题是什么?

public class CameraConnectionFragment extends Fragment {
private static final Logger LOGGER = new Logger();
/**
* The camera preview size will be chosen to be the smallest frame by pixel  size capable of
* containing a DESIRED_SIZE x DESIRED_SIZE square.
*/
private static final int MINIMUM_PREVIEW_SIZE = 320;

/**
* Conversion from screen rotation to JPEG orientation.
*/
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
private static final String FRAGMENT_DIALOG = "dialog";
static {
ORIENTATIONS.append(Surface.ROTATION_0, 90);
ORIENTATIONS.append(Surface.ROTATION_90, 0);
ORIENTATIONS.append(Surface.ROTATION_180, 270);
ORIENTATIONS.append(Surface.ROTATION_270, 180);
}





class KameraSurface extends SurfaceView implements SurfaceHolder.Callback  {
private static final String TAG = "Preview";

SurfaceHolder mHolder;
public Camera camera;

KameraSurface(Context context) {
  super(context);

  // Install a SurfaceHolder.Callback so we get notified when the
  // underlying surface is created and destroyed.
  mHolder = getHolder();
  mHolder.addCallback(this);
  mHolder.setFormat(PixelFormat.RGB_332);
  mHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
}

public void surfaceCreated(SurfaceHolder holder) {
  // The Surface has been created, acquire the camera and tell it where
  // to draw.

  camera = Camera.open();

  Camera.Parameters p = camera.getParameters();
  p.setPictureSize(80, 60);
  p.setColorEffect(android.hardware.Camera.Parameters.EFFECT_NONE);
  p.setJpegQuality(20);
  p.setPreviewFrameRate(1);
  p.setPreviewFpsRange(5, 10);
  p.setPreviewSize(80, 60);
  camera.setParameters(p);



  try {
    camera.setPreviewDisplay(holder);


    camera.setPreviewCallback(new Camera.PreviewCallback() {

      public void onPreviewFrame(byte[] data, Camera arg1) {
        //KameraSurface.this.invalidate();
      }
    });
  } catch (IOException e) {
    e.printStackTrace();
  }
}

public void surfaceDestroyed(SurfaceHolder holder) {
  // Surface will be destroyed when we return, so stop the preview.
  // Because the CameraDevice object is not a shared resource, it's very
  // important to release it when the activity is paused.
  camera.stopPreview();
  camera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
  // Now that the size is known, set up the camera parameters and begin
  // the preview.
  Camera.Parameters parameters = camera.getParameters();
  parameters.setPreviewSize(80, 60);
  camera.setParameters(parameters);

  camera.startPreview();
}

@Override
public void draw(Canvas canvas) {
  super.draw(canvas);
  Paint p= new Paint(Color.RED);
  Log.d(TAG,"draw");
  canvas.drawText("PREVIEW", canvas.getWidth()/2, canvas.getHeight()/2, p  );
}

}









/**
* {@link android.view.TextureView.SurfaceTextureListener} handles several  lifecycle events on a
* {@link TextureView}.
*/

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
View view =  inflater.inflate(R.layout.camera_connection_fragment,  container, false);
Button capture = (Button) view.findViewById(R.id.button);
Button preview = (Button) view.findViewById(R.id.button2);
KameraSurface preview2 = new  KameraSurface(getActivity().getApplicationContext());
((FrameLayout) view.findViewById(R.id.surfaceView2)).addView(preview2);


preview.setOnClickListener(new View.OnClickListener() {


  public void onClick(View v) {




  }
});

  capture.setOnClickListener(new View.OnClickListener() {


      public void onClick(View v) {
          try {
              captureSession.capture(previewRequest, captureCallback, backgroundHandler);
          }
          catch (final CameraAccessException e) {
              LOGGER.e(e, "Exception!");
          }

      }
  });

return view;

}

为了清晰起见,我还添加了xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<org.tensorflow.demo.AutoFitTextureView
    android:id="@+id/texture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />

<org.tensorflow.demo.RecognitionScoreView
    android:id="@+id/results"
    android:layout_width="match_parent"
    android:layout_height="198dp"
    android:layout_alignParentTop="true" />

<org.tensorflow.demo.OverlayView
    android:id="@+id/debug_overlay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="21dp"
    android:layout_marginStart="12dp"
    android:text="Capture" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignTop="@+id/button"
    android:layout_marginEnd="35dp"
    android:text="Preview" />

<SurfaceView
    android:id="@+id/surfaceView2"
    android:layout_width="100dp"
    android:layout_height="133dp"
    android:layout_alignBottom="@+id/button"
    android:layout_alignParentStart="true"
    android:layout_marginStart="134dp"
    android:layout_margin="20dp"/>


</RelativeLayout>

提前致谢。

0 个答案:

没有答案