0

I make a little application on Android for add timestamp to a photo and i have a little issue

 private void openCamera() {
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, "New picture");
    values.put(MediaStore.Images.Media.DESCRIPTION, "New picture");
    uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI , values);

    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT , uri);
    startActivityForResult(cameraIntent , IMAGE_CAPTURE_CODE);
}

when i click to the button , openCamera is called.

All work great , the photo is saved on my phone and the photo is on my imageView

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        imageView.setImageURI(uri);


        Bitmap src = null;
        try {
            src = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:MM:ss");

        String dateTime = sdf.format(Calendar.getInstance().getTime()); // reading local time in the system

        Canvas cs = new Canvas(dest);
        Paint tPaint = new Paint();
        tPaint.setTextSize(100);
        tPaint.setColor(Color.BLUE);
        tPaint.setStyle(Paint.Style.FILL);
        // image qui est tourné , a changer et fini
        cs.drawBitmap(src, 0f, 0f, null);
        float height = tPaint.measureText("yY");
        cs.drawText(dateTime, 20f, height+15f, tPaint);
        try {
            dest.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/sdcard/timeStampedImage.jpg")));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

After , on ActivityResult i want to add the timestamp to the photo.

All works but the photo is rotated when the picture is saved :(

And i don't know why , where on the code , the rotation is modified ?

Thanks you so much for the help :)

Exemple : before https://ibb.co/1nLtJx5 after https://ibb.co/L1MG1FJ

New try

    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        imageView.setImageURI(uri);


        Bitmap src = null;
        try {
            src = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ExifInterface ei = null;
        try (InputStream inputStream = MainActivity.this.getContentResolver().openInputStream(uri)){
            ei = new ExifInterface(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

        Bitmap rotatedBitmap = null;
        switch(orientation) {

            case ExifInterface.ORIENTATION_ROTATE_90:
                rotatedBitmap = rotateImage(src, 90);
                break;

            case ExifInterface.ORIENTATION_ROTATE_180:
                rotatedBitmap = rotateImage(src, 180);
                break;

            case ExifInterface.ORIENTATION_ROTATE_270:
                rotatedBitmap = rotateImage(src, 270);
                break;

            case ExifInterface.ORIENTATION_NORMAL:
            default:
                rotatedBitmap = src;
        }
   
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:MM:ss");

        String dateTime = sdf.format(Calendar.getInstance().getTime()); // reading local time in the system

        Canvas cs = new Canvas(rotatedBitmap);
        Paint tPaint = new Paint();
        tPaint.setTextSize(100);
        tPaint.setColor(Color.BLUE);
        tPaint.setStyle(Paint.Style.FILL);
        // image qui est tourné , a changer et fini
        cs.drawBitmap(src, 0f, 0f, null);
        float height = tPaint.measureText("yY");
        cs.drawText(dateTime, 20f, height+15f, tPaint);
        try {
            rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/sdcard/timeStampedImage.jpg")));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


  public static Bitmap rotateImage(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
            matrix, true);
}

i have made modification with the link of collaborater and i create a bitmap with the orientation of the Uri source

And now the result... The picture is in 2 part ( good orientation and bad orientation ) :( https://ibb.co/C9495PL

1 Answers1

0

Issue Resolved

I use this code

Bitmap src = null;
        try {
            src = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ExifInterface ei = null;
        try (InputStream inputStream = MainActivity.this.getContentResolver().openInputStream(uri)){
            ei = new ExifInterface(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

        Bitmap rotatedBitmap = null;
        switch(orientation) {

            case ExifInterface.ORIENTATION_ROTATE_90:
                rotatedBitmap = rotateImage(src, 90);
                break;

            case ExifInterface.ORIENTATION_ROTATE_180:
                rotatedBitmap = rotateImage(src, 180);
                break;

            case ExifInterface.ORIENTATION_ROTATE_270:
                rotatedBitmap = rotateImage(src, 270);
                break;

            case ExifInterface.ORIENTATION_NORMAL:
            default:
                rotatedBitmap = src;
        }

        Uri uriTest = getImageUri( rotatedBitmap);


        Bitmap dest = Bitmap.createBitmap(rotatedBitmap.getWidth(), rotatedBitmap.getHeight(), Bitmap.Config.ARGB_8888);


   public Uri getImageUri( Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    double random = Math.random() * 1000 + 1;

    String path = MediaStore.Images.Media.insertImage(getContentResolver(), inImage, "picture" + random, null);
    return Uri.parse(path);
}

I create a BitMap "dest" with the bitmap rotated !

  Bitmap dest = Bitmap.createBitmap(rotatedBitmap.getWidth(), rotatedBitmap.getHeight(), Bitmap.Config.ARGB_8888);

And

      Canvas cs = new Canvas(dest);

And draw Bitmap on the rotatedBitmap

  cs.drawBitmap(rotatedBitmap, 0f, 0f, null);

And for save it

        MediaStore.Images.Media.insertImage(getContentResolver() , dest , namePhoto , "Heure sur la photo");