2013-04-30 73 views
13

我想显示使用机器人:取摄像机图像无“保存” /“删除”确认

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

这工作得很好,但迄今为止采取的照片的用户后,从相机的ImageView的拍摄的图像使用选择的相机应用程序会出现一个对话框(可能来自应用程序)询问是否保存或删除拍摄的照片(至少在Android 2.3和4.2使用默认相机应用程序)。

我想跳过这个额外的对话框,并在ImageView的直接显示图像时(onActivityResult被调用),因为这意味着对于用户来说,这是不必要的,因为他将有possibilty到一个额外的互动一步保存或删除我的应用程序中的照片。

这是可能使用简单的ACTION_IMAGE_CAPTURE意图或我需要更复杂的像Camera Preview和SurfaceView这样的目的?

回答

15

您CA使用SurfaceView捕捉图像

package com.camera; 

import java.io.IOException; 

import android.app.Activity; 
import android.content.Intent; 
import android.hardware.Camera; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 

public class Camera_capture extends Activity implements SurfaceHolder.Callback { 

private Camera mCamera; 
private SurfaceView surfaceView; 
private SurfaceHolder surfaceHolder; 
private Button capture_image; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.camera_layout); 
    capture_image = (Button) findViewById(R.id.capture_image); 
    capture_image.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      capture(); 
     } 
    }); 
    surfaceView = (SurfaceView) findViewById(R.id.surfaceview); 
    surfaceHolder = surfaceView.getHolder(); 
    surfaceHolder.addCallback(Camera_capture.this); 
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    try { 
     mCamera = Camera.open(); 
     mCamera.setPreviewDisplay(surfaceHolder); 
     mCamera.startPreview(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private void capture() { 
    mCamera.takePicture(null, null, null, new Camera.PictureCallback() { 

     @Override 
     public void onPictureTaken(byte[] data, Camera camera) { 
      Toast.makeText(getApplicationContext(), "Picture Taken", 
        Toast.LENGTH_SHORT).show(); 
      Intent intent = new Intent(); 
      intent.putExtra("image_arr", data); 
      setResult(RESULT_OK, intent); 
      camera.stopPreview(); 
      if (camera != null) { 
       camera.release(); 
       mCamera = null; 
      } 
      finish(); 
     } 
    }); 
} 

@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, 
     int height) { 
    Log.e("Surface Changed", "format == " + format + ", width === " 
      + width + ", height === " + height); 
    try { 
     mCamera.setPreviewDisplay(holder); 
     mCamera.startPreview(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    Log.e("Surface Created", ""); 
} 

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 
    Log.e("Surface Destroyed", ""); 
} 

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

而且布局文件是

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

<SurfaceView 
    android:id="@+id/surfaceview" 
    android:layout_width="fill_parent" 
    android:layout_weight="100" 
    android:layout_height="wrap_content" /> 

<Button 
    android:id="@+id/capture_image" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="Capture" /> 

</LinearLayout> 

开始startActivityForResult这个Camera_capture活动和onActivityResult你可以得到图像为byte阵列为

byte[] image = data.getExtras().getByteArray("image_arr"); 

其中data是接收的数据。

解码的byte阵列Bitmap使用

Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, 
        image.length); 

现在设置这个Bitmap


编辑

由于没有在返回byte[]一些问题,byte[]应该被保存在一个文件和p文件的运行时间应该被发送到前面的Activity,以便可以读取文件。

onPictureTaken(),只需添加

String PATH = "Any path to store a file"; 
try { 
    FileOutputStream fos=new FileOutputStream(PATH); 

    fos.write(data); 
    fos.close(); 
    } 
    catch (java.io.IOException e) { 

    } 

和到位的:

intent.putExtra("image_arr", data); 

intent.putExtra("image_path", PATH); 

,并接受以前ActivityonActivityResult因为这个路径:

String imagePath = data.getExtras().getString("image_path"); 
+1

谢谢!这给了我一个好主意,它将如何工作。我注意到,使用上面的代码图像旋转了90度,而且在调用finish()后,acitivity没有关闭,所以它永远不会回到主要活动 - 应用程序挂起... – fritz 2013-04-30 12:47:48

+0

欢迎.. :) 乐于帮助.. – 2013-04-30 12:59:39

+0

将检查您的问题。 – 2013-04-30 13:04:47

-2

使用MediaStore.EXTRA_OUTPUT参数有关您的相机应用程序中运行的意图

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
+0

你确定这满足了有机磷农药的愿望绕过保存/删除确认。它不适合我。 – paulkayuk 2013-04-30 10:36:28

+0

这没有帮助,虽然我添加了intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, \t \t Uri.fromFile(new File(Environment.getExternalStorageDirectory()+“/ image11.jpg”)) ); – fritz 2013-04-30 10:44:50

+0

这里是官方文档的链接http://developer.android.com/guide/topics/media/camera.html#intent-image – httpdispatch 2013-04-30 10:55:59

2

如果图像旋转时,您可以使用此:

mCamera.setDisplayOrientation(90); 

mCamera.startPreview(); 
1

可以使用AnujMathur_07的解决方案,但如果你不想与路径和文件工作在方法onPictureTaken,您可以返回位图:

@Override 
      public void onPictureTaken(byte[] data, Camera camera) { 
       Toast.makeText(getApplicationContext(), "Picture Taken", 
         Toast.LENGTH_SHORT).show(); 
       Intent intent = new Intent(); 

      Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, 
        data.length); 

      intent.putExtra("image", bmp); 
      setResult(RESULT_OK, intent); 
      camera.stopPreview(); 
      if (camera != null) { 
       camera.release(); 
       mCamera = null; 
      } 
      finish(); 
     } 

而且我n中的方法onActivityResult你可以得到位图:

Bundle extras = data.getExtras(); 
Bitmap imageBitmap = (Bitmap) extras.get("image"); 
6

使用“android.intent.extra.quickCapture”

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
intent.putExtra("android.intent.extra.quickCapture",true); 
if (intent.resolveActivity(getPackageManager()) != null) { 
    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); 
} 
+0

谢谢,这对我有用。 – 2017-07-27 11:50:56

+0

没有改变任何东西,虽然那本来不错 – Twometer 2017-10-13 10:02:26