0

i have tried to implement the concept to capture image via front camera.So, i have followed the step of the below site http://www.vogella.com/tutorials/AndroidCamera/article.html but i have encounter the issue while click the button. Can you some one help me on this please.

ImagePickActivity

   public class ImagePickActivity extends Activity {

    private Camera camera;
    private int cameraId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_pick);

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

      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("Camera", "Camera found");
                cameraId = i;
                break;
              }
            }
            return cameraId;
          }



      @Override
      protected void onPause() {
        if (camera != null) {
          camera.release();
          camera = null;
        }
        super.onPause();
      }
 }




public class PhotoHandler implements PictureCallback {

      private final Context context;

      public PhotoHandler(Context context) {
        this.context = context;
      }

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        // TODO Auto-generated method stub

        File pictureFileDir = getDir();

        if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {

          Log.d("Camara", "Can't create directory to save image.");
          Toast.makeText(context, "Can't create directory to save image.",
              Toast.LENGTH_LONG).show();
          return;
     }


        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
        String date = dateFormat.format(new Date());
        String photoFile = "Picture_" + date + ".jpg";

        String filename = pictureFileDir.getPath() + File.separator + photoFile;

        File pictureFile = new File(filename);

        try {
          FileOutputStream fos = new FileOutputStream(pictureFile);
          fos.write(data);
          fos.close();
          Toast.makeText(context, "New Image saved:" + photoFile,
              Toast.LENGTH_LONG).show();
        } catch (Exception error) {
          Log.d("Camara", "File" + filename + "not saved: "
              + error.getMessage());
          Toast.makeText(context, "Image could not be saved.",
              Toast.LENGTH_LONG).show();
        }
      }

      private File getDir() {
            File sdDir = Environment
              .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            return new File(sdDir, "CameraAPIDemo");
          }
     } 

LogCat

FATAL EXCEPTION: main Process: com.frontcamera, 
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3848)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18770)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:82)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3843)
Caused by: java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:1597)
at android.hardware.Camera.takePicture(Camera.java:1542)
at com.frontcamera.ImagePickActivity.onClick(ImagePickActivity.java:44)
4

3 回答 3

0

在调用 takePicture 之前,您必须显示相机预览。如果你想在不显示预览的情况下拍照,你可以做这个技巧:显示预览,而不是在屏幕上。搜索这个论坛,因为它已经在 stackoverflow 上讨论过。

于 2016-02-24T11:09:53.363 回答
0

我认为您正在混合隐式和显式意图。

vogella 的代码是这样做的:显示相机预览并在单击按钮时拍照而不关闭应用程序。

您正在使用的意图: Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 这是隐含的意图,您让用户使用他/她选择的应用程序(Instagram-Native 相机等)拍照。这将退出您的应用程序,并在使用照片数据拍摄照片时返回。

你应该首先决定做这些事情之一。

于 2016-02-24T19:57:58.133 回答
0

向您的清单添加权限。

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


也看看这里 - java.lang.RuntimeException: takePicture failed

于 2016-02-24T11:11:55.600 回答