2014-05-16 57 views
0

ALL,检索摄像机图像上的Android

我有以下代码:

try 
    { 
     Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     File storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
     cameraImageFiles = File.createTempFile("user_photo", ".png", storage); 
     captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraImageFiles)); 
     final Intent galleryIntent = new Intent(); 
     galleryIntent.setType("image/*"); 
     galleryIntent.setAction(Intent.ACTION_GET_CONTENT); 
     Intent photoIntent = Intent.createChooser(galleryIntent, "Select or take a new picture"); 
     photoIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { captureIntent }); 
     startActivityForResult(photoIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 
    } 
    catch(IOException e) 
    { 
     Utils.displayErrorDialog(context, e.getMessage()); 
    } 

在网络上的每一个例子,在这里谈谈从摄像机获取图像时,只有1意图。

在这里,我有一个IntentChooser来选择图库图像或使用相机拍摄新照片。据我了解使用这段代码,我不能简单地获取图像,因为在“onActivityResult()”我应该将图片保存到文件 - 它不会自动保存。

现在我想要做的就是从相机拍摄图像。我真的不在乎它是否会被保存 - 我只是想要一张照片。

我知道如何从图库中获取图像 - 我有这个代码,它的工作原理。但从相机获取图像现在是一个难题。

谢谢。

[编辑] 这是我放在onActivityResult()的代码

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) 
    { 
     if(resultCode == RESULT_OK) 
     { 
      Uri uri = null; 
      if(data == null) 
      { 
       if(cameraImageFiles.exists()) 
       { 
        uri = Uri.fromFile(cameraImageFiles); 
        InputStream input; 
        try 
        { 
         input = getContentResolver().openInputStream(uri); 
         BitmapFactory.Options opts = new BitmapFactory.Options(); 
         opts.inJustDecodeBounds = true; 
         bmp = BitmapFactory.decodeStream(input, null, opts); 
         input.close(); 
         int height = opts.outHeight; 
         int width = opts.outWidth; 
         int inSampleSize = 1; 
         int reqHeight = camera.getHeight(); 
         int reqWidth = camera.getWidth(); 
         if(height > reqHeight || width > reqWidth) 
         { 
          int halfHeight = height/2; 
          int halfWidth = width/2; 
          while((halfHeight/inSampleSize) > reqHeight && (halfWidth/inSampleSize) > reqWidth) 
           inSampleSize *= 2; 
         } 
         opts.inSampleSize = inSampleSize; 
         opts.inJustDecodeBounds = false; 
         bmp = BitmapFactory.decodeStream(input, null, opts); 
         camera.setImageBitmap(bmp); 
        } 
        catch(FileNotFoundException e) 
        { 
         Utils.displayErrorDialog(this, e.getMessage()); 
        } 
        catch(IOException e) 
        { 
         Utils.displayErrorDialog(this, e.getMessage()); 
        } 
       } 
       photoReady = true; 
      } 

执行后,代码给出空为bmp,所以我想不保存图像。

[/编辑]

+0

它被保存。否则无法获得照片。 – CommonsWare

+0

您需要使用SurfaceView创建自己的相机。 – njzk2

+0

为什么有意在画廊或照相机之间进行选择?这没有任何意义,为什么你没有提出一个对话框,并在对话结果 – Tascalator

回答

0

相机将节省磁盘上的所拍摄的画面,但不会是PNG,将JPEG格式。此外,如果使用ACTION_IMAGE_CAPTURE,数据将不会为空,因此您的代码根本无法工作。此外,相机捕获意图应该仔细启动,以避免寻找许多设备的well documented bug。请参阅https://stackoverflow.com/a/16433874/192373及其附近。

注意ACTION_GET_CONTENT需要在奇巧特殊处理,你可能会ACTION_OPEN_DOCUMENT得到更好的服务,请参阅https://stackoverflow.com/a/20177611/192373

+0

好吧,所以我最好用我自己的对话框进行动作选择?因为它看起来像标准的一个太麻烦了...... – Igor

+0

我不认为系统选择器对话会让你的生活变得更难。即使你打开了自己的对话框,你仍然需要正确处理这两个操作,并准备好在Kit-Kat中引入的更改。 –

+0

所以让我重申一遍。拍摄照片后,系统将自行将其保存到扩展名为“.jpeg”的文件中。我不需要将它保存在onActivityResult()中,我只需要将此文件加载到内存中。现在Kit-Kat的变化是否向后兼容?这意味着我将能够使用以前版本的代码.... – Igor