简体   繁体   English

使用相机拍摄照片,但不将其另存为文件,仅显示

[英]Take a photo with Camera but don't save it as a file, only show it

In my Android project I want to take a photo with Camera but don't save it as a file, only show it on a ImageView , as a drawable or other. 在我的Android项目中,我想用Camera拍照, 但不要将其另存为文件,而只能在ImageView上以可绘制或其他形式显示。

In my project I can see the camera, like the code below. 在我的项目中,可以看到相机,如下面的代码所示。

I tryied a lot of ways, but I cant take the photo without save it SDCard. 我尝试了很多方法,但是如果不保存SDCard卡就无法拍照。

ShowCamera.java ShowCamera.java

 public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {

    private SurfaceHolder holdMe;
    private Camera theCamera;

    public ShowCamera(Context context, Camera camera) {
        super(context);
        theCamera = camera;
        holdMe = getHolder();
        holdMe.addCallback(this);
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            theCamera.setPreviewDisplay(holder);
            theCamera.startPreview();
        } catch (IOException e) {
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
    }
}

MainActivity.java MainActivity.java

public class MainActivity extends Activity  {

       private Camera cameraObject;
       private ShowCamera showCamera;

       public static Camera isCameraAvailiable(){
          Camera object = null;
          try {
             object = Camera.open(); 
          }
          catch (Exception e){
          }
          return object; 
       }



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

          cameraObject = isCameraAvailiable();
          showCamera = new ShowCamera(this, cameraObject);
          final FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
          preview.addView(showCamera);
    }

Thanks! 谢谢!

First of all, do not forget to put this in your AndroidManifest.xml: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 首先,不要忘记将其放在您的AndroidManifest.xml中: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

So, you can follow this example: http://hmkcode.com/android-camera-taking-photos-camera/ 因此,您可以按照以下示例操作: http : //hmkcode.com/android-camera-taking-photos-camera/

You need to include this line inside ur button click routine: 您需要在您的按钮单击例程中包括以下行:

mCamera.takePicture(null, null, mPicture); mCamera.takePicture(null,null,mPicture);

And then create a routine: 然后创建一个例程:

private PictureCallback mPicture = new PictureCallback(){ //+ a lot of things...}

The code below should be inside the PictureCallBack routine 下面的代码应该在PictureCallBack例程中

public void onPictureTaken(byte[] data, Camera camera){
   File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
      try{
         FileOutputStream fos = new FileOutputStream(pictureFile);
         fos.write(data);
         fos.close();
      }
      catch(FileNotFoundException e){}
      catch(IOException e){}
      File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                                      Environment.DIRECTORY_PICTURES), "MyCameraApp");
}

And try to find the function: 并尝试找到该功能:

private static File getOutputMediaFile(int type){}

That also should be included! 那也应该包括在内!

So you need an image taken from camera and show it on a ImageView , not to save it? 因此,您需要从相机拍摄的图像并将其显示在ImageView而不是保存它?

Create a picture callback to receive data from camera and decode it to Bitmap: 创建图片回调以接收来自相机的数据并将其解码为Bitmap:

private PictureCallback mPicture = new PictureCallback() {

    @Override
    public void onPictureTaken(final byte[] data, Camera c) {
        Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
        yourImageView.setImageBitmap(bm);
    }
};

Call camera capture (when capture button clicked): 呼叫摄像机捕获(单击捕获按钮时):

try {
    mCamera.takePicture(null, null, mPicture);
} catch (Exception e) {
    // take picture failed
    e.printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM