Camera picture to Bitmap results in messed up image

后端 未结 7 1870
谎友^
谎友^ 2020-12-29 10:00

This is my code that I use to save the image to a Bitmap. This piece of code is based on the code from CyanogenMod\'s camera app so I would assume it would work

相关标签:
7条回答
  • 2020-12-29 10:30

    This is my code, that i use to take pictures, and save them in a database, without changing its quality.

    First you need this variables:

     private static final int CAMERA_PIC_REQUEST = 2500;
    private Rect Padding = new Rect(-1,-1,-1,-1);
    private Matrix matrix = new Matrix();
    private Bitmap rotatedBitmap = null;
    private Bitmap bm = null;
    private Bitmap scaledBitmap= null;
    private ExifInterface exif ;
    private int rotation=0;
    private final BitmapFactory.Options options = new BitmapFactory.Options();
    

    Then set the request, from a button, activity or fragment, depending on your app (Mine uses a button)

    btn_camera.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
    
                  Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                  startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
    
              }
        }
        );
    

    Now as I am requesting the CAMERA _PIC_REQUEST from a Fragment i implement this code on the OnActivityResult()

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST)
            if (data != null) {
            imageuri = data.getData();
            //Bitmap image = (Bitmap) data.getExtras().get("data");
    
            try {
                options.inSampleSize = 2;
                exif = new ExifInterface(getRealPathFromURI(imageuri));
                rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                rotation = exifToDegrees(rotation);
    
                bm = BitmapFactory.decodeStream(
                        getActivity().getContentResolver().openInputStream(imageuri),Padding,options);
    
                scaledBitmap = Bitmap.createScaledBitmap(bm, 400,400, true);
    
                if (rotation != 0)
                {matrix.postRotate(rotation);}
                //matrix.postRotate(180);
                rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            mIvFoto.setImageBitmap(rotatedBitmap);
    
    
         }
     }
    

    The rotation and the matrix are for scaling the pic taken, to an ImageView, so i can take a preview of the photo, before saving it. That doesn't affect the quality of the pic. It's just for the preview bitmap. So in the ScaledBitmap variable as you can see, when CreatingScaledBitmap, i place the dimensions of the pic, to 400,400 you can place the dimensions of the pic you want you get, be sure to rotate the bitmap, so you can get the picture right, even if you take the photo in portrait mode.

    Finally the methods for rotating and getting the picture from the Path that the camera saves it.

    private static int exifToDegrees(int exifOrientation) {
        if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; }
        else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {  return 180; }
        else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {  return 270; }
        return 0;
    }
    
    private String getRealPathFromURI(Uri contentURI) {
        String result;
        Cursor cursor =getActivity().getContentResolver().query(contentURI, null, null, null, null);
        if (cursor == null) { // Source is Dropbox or other similar local file path
            result = contentURI.getPath();
        } else {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            result = cursor.getString(idx);
            cursor.close();
        }
        return result;
    }
    

    I hope this may help you.

    0 讨论(0)
提交回复
热议问题