Android 中默认拍照的相机

发布于 2024-11-06 02:40:01 字数 27 浏览 2 评论 0原文

如何在android中使用默认相机拍照?

How to use default camera to take a picture in android ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿挽旧人 2024-11-13 02:40:01
Uri imageUri;
final int TAKE_PICTURE = 115;

public void capturePhoto(View view) {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photoFile = new File(Environment.getExternalStorageDirectory(),  "Photo.png");
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(photoFile));
    imageUri = Uri.fromFile(photoFile);
    startActivityForResult(intent, TAKE_PICTURE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case TAKE_PICTURE:
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedImageUri = imageUri;
                //Do what ever you want
        }
    }
}
Uri imageUri;
final int TAKE_PICTURE = 115;

public void capturePhoto(View view) {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photoFile = new File(Environment.getExternalStorageDirectory(),  "Photo.png");
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(photoFile));
    imageUri = Uri.fromFile(photoFile);
    startActivityForResult(intent, TAKE_PICTURE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case TAKE_PICTURE:
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedImageUri = imageUri;
                //Do what ever you want
        }
    }
}
星光不落少年眉 2024-11-13 02:40:01

用于打开相机的意图是

 buttonCapturePhoto.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, CAPTURE_IMAGE);
        }
    });

捕获后为您提供图像的代码是

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Uri uriImage;
    InputStream inputStream = null;
    if ( (requestCode == SELECT_IMAGE || requestCode == CAPTURE_IMAGE) && resultCode == Activity.RESULT_OK) {
        uriImage = data.getData();
        try {
            inputStream = getContentResolver().openInputStream(uriImage);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
            imageView.setImageBitmap(bitmap);
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setAdjustViewBounds(true);
    }
}

The intent which is used to open the camera is

 buttonCapturePhoto.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, CAPTURE_IMAGE);
        }
    });

The code which gives you the image after capturing is

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Uri uriImage;
    InputStream inputStream = null;
    if ( (requestCode == SELECT_IMAGE || requestCode == CAPTURE_IMAGE) && resultCode == Activity.RESULT_OK) {
        uriImage = data.getData();
        try {
            inputStream = getContentResolver().openInputStream(uriImage);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
            imageView.setImageBitmap(bitmap);
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setAdjustViewBounds(true);
    }
}

沩ん囻菔务 2024-11-13 02:40:01

这是一个简单的例子。无论如何,这将返回图像作为一个小位图。如果你想检索全尺寸的图像,那就有点复杂了。

ImageView  takePhotoView = (ImageView) findViewById(R.id.iwTakePicture);
Bitmap imageBitmap = null;
takePhotoView.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub              
                dispatchTakePictureIntent(0);
            }
        });

    private void dispatchTakePictureIntent(int actionCode) {
          Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);     
          startActivityForResult(takePictureIntent, actionCode);      
        }

    private void handleSmallCameraPhoto(Intent intent) {
        Bundle extras = intent.getExtras();
        this.imageBitmap = (Bitmap) extras.get("data");
        takePhotoView.setImageBitmap(imageBitmap);
    }

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        if(resultCode == RESULT_OK)
            handleSmallCameraPhoto(data);
   }

This is a simple example.Anyway this will return the image as a small bitmap.If you want to retrive the full-sized image ,is a bit more complicated.

ImageView  takePhotoView = (ImageView) findViewById(R.id.iwTakePicture);
Bitmap imageBitmap = null;
takePhotoView.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub              
                dispatchTakePictureIntent(0);
            }
        });

    private void dispatchTakePictureIntent(int actionCode) {
          Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);     
          startActivityForResult(takePictureIntent, actionCode);      
        }

    private void handleSmallCameraPhoto(Intent intent) {
        Bundle extras = intent.getExtras();
        this.imageBitmap = (Bitmap) extras.get("data");
        takePhotoView.setImageBitmap(imageBitmap);
    }

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        if(resultCode == RESULT_OK)
            handleSmallCameraPhoto(data);
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文