【问题标题】:How to capture a photo from the camera without intent如何在没有意图的情况下从相机拍摄照片
【发布时间】:2013-08-17 17:42:37
【问题描述】:

我希望我的应用能够在不使用其他应用的情况下拍摄照片。我使用的代码:

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

但此代码使用手机的主摄像头应用。谁能给我一些代码?

【问题讨论】:

    标签: android android-intent camera image-capture


    【解决方案1】:

    直接使用Camera 类拍照非常复杂。

    我正在开发a library to simplify this,您只需将CameraFragment 添加到您的应用中以获得基本的预览UI,然后调用takePicture() 来拍照,并通过各种方式来配置行为(例如,图片保存在哪里)。但是,这个库仍在进行中。

    谁能给我一些代码?

    “某些代码”将长达数千行(对于完整的实现,包括处理各种特定于设备的怪事)。

    欢迎阅读the Android developer documentation on the subject

    【讨论】:

    • 我有一个使用 mediarecorder 来录制视频的应用程序,它并不大。拍照有没有类似的东西?我想要的是获取一个图像并用另一个位图覆盖它以创建一个文件。请查看我关于这个问题的完整问题:stackoverflow.com/questions/18289544/…
    • 如果我用 camera = Camera.open(); 打开相机怎么办?并使用文档中看到的 Camera.takePicture() ?
    • @mremremre1:“我有一个使用 mediarecorder 录制视频的应用程序,而且它不是那么大”——那么我怀疑你没有处理所有设备。单独获得正确的预览,对于纵向/横向加背面/FFC,需要很多。 “如果我用 camera = Camera.open(); 打开相机并使用文档中看到的 Camera.takePicture() 会怎样?” -- 这当然是起点。
    • 谢谢,我会试试的。你看到另一个问题了吗?因为如果有办法直接从 SurfaceView 获取屏幕截图,我就不必使用相机 api
    • @mremremre1:AFAIK,你不能截取SurfaceView 的屏幕截图。
    【解决方案2】:

    设置好相机预览后,您需要执行以下操作...

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

    还有getOutputMediaFile函数:

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

    你完成了!!!

    找到它here

    【讨论】:

    • 感谢上帝:D
    【解决方案3】:

    相机在 API 21 中被弃用,新的​​方式是使用android.hardware.camera2

    要枚举、查询和打开可用的相机设备,请获取CameraManager 实例。

    快速总结:

    1. 通过调用Context.getSystemService(String)获取相机管理器实例
    2. 通过调用 CameraManager.GetCameraIdList() 获取设备摄像头 ID 的 string[]
    3. 使用上一步中所需的相机 ID 调用 CameraManager.OpenCamera(...)。

    一旦打开相机,就会调用 OpenCamera(...) 中提供的回调。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      相关资源
      最近更新 更多