How to capture a photo from the camera without intent

China☆狼群 提交于 2019-12-19 02:32:16

问题


I want my app to be able to capture photos without using another application. The code i used :

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File photo = null;
    try
    {
        photo = this.createTemporaryFile("picture", ".jpg");
        photo.delete();
    }
    catch(Exception e)
    {
        Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();

    }
    mImageUri = Uri.fromFile(photo);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

But this code uses the phone's main camera app. Can anyone give me some code ?


回答1:


Taking a picture directly using the Camera class is insanely complicated to get right.

I am working on a library to simplify this, where you just add a CameraFragment to your app for the basic preview UI, and call takePicture() on it to take a picture, with various ways to configure the behavior (e.g., where the pictures get saved). However, this library is still a work in progress.

Can anyone give me some code ?

"Some code" is going to be thousands of lines long (for a complete implementation, including dealing with various device-specific oddities).

You are welcome to read the Android developer documentation on the subject.




回答2:


once you have the camera preview set, you need to do the following...

protected static final int MEDIA_TYPE_IMAGE = 0; 

public void capture(View v)
{   
    PictureCallback pictureCB = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera cam) {
          File picFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
          if (picFile == null) {
            Log.e(TAG, "Couldn't create media file; check storage permissions?");
            return;
          }

          try {
            FileOutputStream fos = new FileOutputStream(picFile);
            fos.write(data);
            fos.close();
          } catch (FileNotFoundException e) {
            Log.e(TAG, "File not found: " + e.getMessage());
            e.getStackTrace();
          } catch (IOException e) {
            Log.e(TAG, "I/O error writing file: " + e.getMessage());
            e.getStackTrace();
          }
        }
      };
      camera.takePicture(null, null, pictureCB);
}

And the getOutputMediaFile function:

private File getOutputMediaFile(int type) 
{
      File dir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), getPackageName());
      if (!dir.exists()) 
      {
        if (!dir.mkdirs()) 
        {
          Log.e(TAG, "Failed to create storage directory.");
          return null;
        }
      }
      String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss", Locale.UK).format(new Date());
      if (type == MEDIA_TYPE_IMAGE) 
      {
        return new File(dir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg");
      } 
      else 
      {
        return null;
      }
}

And you are done!!!

found it here




回答3:


Camera was deprecated in API 21, the new way is the use android.hardware.camera2.

To enumerate, query, and open available camera devices, obtain a CameraManager instance.

To quickly summarize:

  1. Obtain a camera manager instance by calling Context.getSystemService(String)
  2. Get a string[] of device camera IDs by calling CameraManager.GetCameraIdList().
  3. Call CameraManager.OpenCamera(...) with the desired camera ID from the previous step.

Once the camera is opened, the callback provided in OpenCamera(...) will be called.



来源:https://stackoverflow.com/questions/18291630/how-to-capture-a-photo-from-the-camera-without-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!