Capture Image from Camera and Display in Activity

前端 未结 16 1005
悲&欢浪女
悲&欢浪女 2020-11-21 06:43

I want to write a module where on a click of a button the camera opens and I can click and capture an image. If I don\'t like the image I can delete it and click one more i

16条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 07:01

    Here is code I have used for Capturing and Saving Camera Image then display it to imageview. You can use according to your need.

    You have to save Camera image to specific location then fetch from that location then convert it to byte-array.

    Here is method for opening capturing camera image activity.

    private static final int CAMERA_PHOTO = 111;
    private Uri imageToUploadUri;
    
    private void captureCameraImage() {
            Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File f = new File(Environment.getExternalStorageDirectory(), "POST_IMAGE.jpg");
            chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            imageToUploadUri = Uri.fromFile(f);
            startActivityForResult(chooserIntent, CAMERA_PHOTO);
        }
    

    then your onActivityResult() method should be like this.

    @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
    
                if (requestCode == CAMERA_PHOTO && resultCode == Activity.RESULT_OK) {
                    if(imageToUploadUri != null){
                        Uri selectedImage = imageToUploadUri;
                        getContentResolver().notifyChange(selectedImage, null);
                        Bitmap reducedSizeBitmap = getBitmap(imageToUploadUri.getPath());
                        if(reducedSizeBitmap != null){
                            ImgPhoto.setImageBitmap(reducedSizeBitmap);
                            Button uploadImageButton = (Button) findViewById(R.id.uploadUserImageButton);
                              uploadImageButton.setVisibility(View.VISIBLE);                
                        }else{
                            Toast.makeText(this,"Error while capturing Image",Toast.LENGTH_LONG).show();
                        }
                    }else{
                        Toast.makeText(this,"Error while capturing Image",Toast.LENGTH_LONG).show();
                    }
                } 
            }
    

    Here is getBitmap() method used in onActivityResult(). I have done all performance improvement that can be possible while getting camera capture image bitmap.

    private Bitmap getBitmap(String path) {
    
            Uri uri = Uri.fromFile(new File(path));
            InputStream in = null;
            try {
                final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
                in = getContentResolver().openInputStream(uri);
    
                // Decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(in, null, o);
                in.close();
    
    
                int scale = 1;
                while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) >
                        IMAGE_MAX_SIZE) {
                    scale++;
                }
                Log.d("", "scale = " + scale + ", orig-width: " + o.outWidth + ", orig-height: " + o.outHeight);
    
                Bitmap b = null;
                in = getContentResolver().openInputStream(uri);
                if (scale > 1) {
                    scale--;
                    // scale to max possible inSampleSize that still yields an image
                    // larger than target
                    o = new BitmapFactory.Options();
                    o.inSampleSize = scale;
                    b = BitmapFactory.decodeStream(in, null, o);
    
                    // resize to desired dimensions
                    int height = b.getHeight();
                    int width = b.getWidth();
                    Log.d("", "1th scale operation dimenions - width: " + width + ", height: " + height);
    
                    double y = Math.sqrt(IMAGE_MAX_SIZE
                            / (((double) width) / height));
                    double x = (y / height) * width;
    
                    Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x,
                            (int) y, true);
                    b.recycle();
                    b = scaledBitmap;
    
                    System.gc();
                } else {
                    b = BitmapFactory.decodeStream(in);
                }
                in.close();
    
                Log.d("", "bitmap size - width: " + b.getWidth() + ", height: " +
                        b.getHeight());
                return b;
            } catch (IOException e) {
                Log.e("", e.getMessage(), e);
                return null;
            }
        }
    

    Hope it helps!

提交回复
热议问题