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

sycxhyv7  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(352)

我在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);
}

当我点击按钮时,会调用opencamera。
一切都很好,照片保存在我的手机上,照片在我的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();
        }
    }
}

之后,在activityresult上,我想将时间戳添加到照片中。
保存图片时,除照片外,其他所有功能均正常:(
我不知道为什么,在代码上,旋转被修改了?
非常感谢您的帮助:)
例如:之前https://ibb.co/1nltjx5 之后https://ibb.co/l1mg1fj
新尝试

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);
}

我用collabor的链接进行了修改,并创建了一个带有uri源方向的位图
现在结果。。。图片分为两部分(好方向和坏方向):(https://ibb.co/c9495pl

aiqt4smr

aiqt4smr1#

问题已解决
我用这个密码

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);
}

我创建一个位图“dest”与位图旋转!

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

Canvas cs = new Canvas(dest);

并在旋转位图上绘制位图

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

为了拯救它

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

相关问题