2013-03-21 132 views
0

我正在開發一個android應用程序。我正在使用相機捕捉圖像。我正在使用下面的代碼打開相機。問題是,攝像機視圖在執行後不能打開camera = Camera.open(cameraId)。單擊「製作照片」後,只有相機指示燈已打開(相機視圖未打開)。我如何顯示相機視圖?Android攝像頭視圖未打開

在MakePhotoActivity.java,

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // do we have a camera? 
     if (!getPackageManager() 
       .hasSystemFeature(PackageManager.FEATURE_CAMERA)) { 
      Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG) 
        .show(); 
     } else { 
      cameraId = findFrontFacingCamera(); 
      if (cameraId < 0) { 
       Toast.makeText(this, "No front facing camera found.", 
         Toast.LENGTH_LONG).show(); 
      } else { 
       camera = Camera.open(cameraId); 
      } 
     } 
    } 

public void onClick(View view) { 
     camera.takePicture(null, null, 
       new PhotoHandler(getApplicationContext())); 
    } 

main.xml中,

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

    <Button 
     android:id="@+id/captureFront" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:onClick="onClick" 
     android:text="Make Photo" /> 

</RelativeLayout> 
+0

你嘗試過這一些其他的設備上?它可能是您的設備問題 – 2013-03-21 18:55:56

+0

感謝您的答覆。我也檢查過仿真器,android平板電腦,HTC手機。但相機視圖不能打開。 – Manoj 2013-03-21 19:33:54

回答

1

此代碼是否幫助?

這是因爲雖然面對鏡頭前。

import android.hardware.Camera; 
import android.hardware.Camera.CameraInfo; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Toast; 

public class VideoFragment extends Fragment { 

private static final String TAG = "VideoFragment"; 
private SurfaceView preview; 
private SurfaceHolder previewHolder; 
private Camera camera = null; 
private boolean cameraConfigured = false; 
private boolean inPreview = false; 
private int frontFacingCameraID; 

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

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.video, container, false); 

    preview = (SurfaceView) view.findViewById(R.id.surfaceView1); 
    previewHolder = preview.getHolder(); 
    previewHolder.addCallback(surfaceCallback); 

    return view; 
} 

private int findFrontFacingCamera() { 
    int cameraId = -1; 
    // Search for the front facing camera 
    int numberOfCameras = Camera.getNumberOfCameras(); 
    for (int i = 0; i < numberOfCameras; i++) { 
     CameraInfo info = new CameraInfo(); 
     Camera.getCameraInfo(i, info); 
     if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { 
      Log.d(TAG, "Camera found"); 
      cameraId = i; 
      break; 
     } 
    } 
    return cameraId; 
} 

@Override 
public void onResume() { 
    super.onResume(); 

    camera = Camera.open(frontFacingCameraID); 
    startPreview(); 
} 

@Override 
public void onPause() { 
    if (inPreview) { 
     camera.stopPreview(); 
    } 

    camera.release(); 
    camera = null; 
    inPreview = false; 

    super.onPause(); 
} 

private SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() { 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     // no-op 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     // no-op -- wait until surfaceChanged() 
     // TODO understand why this has to be NO-OP 

    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) { 
     // TODO understand why these methods have to be called in this 
     // callback method 
     initPreview(width, height); 
     startPreview(); 
    } 
}; 

private void initPreview(int width, int height) { 
    if (camera != null && previewHolder.getSurface() != null) { 
     try { 
      camera.setPreviewDisplay(previewHolder); 
     } catch (Throwable t) { 
      Log.e("PreviewDemo-surfaceCallback", 
        "Exception in setPreviewDisplay()", t); 
      Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_LONG) 
        .show(); 
     } 

     if (!cameraConfigured) { 
      Camera.Parameters parameters = camera.getParameters(); 
      Camera.Size size = getBestPreviewSize(width, height, parameters); 

      if (size != null) { 
       parameters.setPreviewSize(size.width, size.height); 
       camera.setParameters(parameters); 
       cameraConfigured = true; 
      } 
     } 
    } 
} 

private void startPreview() { 
    if (cameraConfigured && camera != null) { 
     camera.startPreview(); 
     inPreview = true; 
    } 
} 

private Camera.Size getBestPreviewSize(int width, int height, 
     Camera.Parameters parameters) { 
    Camera.Size result = null; 

    for (Camera.Size size : parameters.getSupportedPreviewSizes()) { 
     if (size.width <= width && size.height <= height) { 
      if (result == null) { 
       result = size; 
      } else { 
       int resultArea = result.width * result.height; 
       int newArea = size.width * size.height; 

       if (newArea > resultArea) { 
        result = size; 
       } 
      } 
     } 
    } 

    return (result); 
} 
} 
+0

感謝您的回覆。我將使用此代碼。 – Manoj 2013-03-21 19:32:08

相關問題