简体   繁体   English

在 Android 上保存位图时出现问题

[英]Issue when saving Bitmap on Android

I make a little application on Android for add timestamp to a photo and i have a little issue我在 Android 上做了一个小应用程序来为照片添加时间戳,但我有一个小问题

 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.当我单击按钮时,会调用 openCamera。

All work great , the photo is saved on my phone and the photo is on my imageView一切都很好,照片保存在我的手机上,照片在我的 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.之后,在 ActivityResult 上,我想将时间戳添加到照片中。

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例:前https://ibb.co/1nLtJx5https://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我已经使用合作者的链接进行了修改,并创建了一个带有 Uri 源方向的位图

And now the result... The picture is in 2 part ( good orientation and bad orientation ) :( https://ibb.co/C9495PL现在结果......图片分为两部分(方向好和方向不好):( https://ibb.co/C9495PL

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 !我创建了一个位图旋转的位图“dest”!

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

And

      Canvas cs = new Canvas(dest);

And draw Bitmap on the rotatedBitmap并在rotatedBitmap上绘制Bitmap

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

And for save it为了保存它

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

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

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