【问题标题】:how to save the correct rotation of the Bitmap如何保存位图的正确旋转
【发布时间】:2017-07-08 12:40:14
【问题描述】:

我遇到了这个问题:当我使用以下代码加载从相机或画廊获取的照片时:

if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_TAKE_PHOTO || requestCode == REQUEST_PICK_PHOTO)
        //dalla fotocamera
        {
            if (data != null)mMediaUri = data.getData();

            Glide.with(this).load(mMediaUri).bitmapTransform(new CenterCrop(context), new RoundCornerTransformation(context, 15, 2)).into(photo);
        }
}

图片在imageView中以正确的方式显示。 否则,我需要压缩位图,所以我制作了这段代码来进行压缩:

 fun compressBitmapInBackgroundtoByteArray(uri: Uri?,context: Context, callback: OnFinishedCallback) {

    Observable.create<ByteArray> { s ->


        var imageStream: InputStream? = null
        try {
            imageStream = context.getContentResolver().openInputStream(uri)
        } catch (e: FileNotFoundException) {
            s.onError(e)
        }

        val bmp = BitmapFactory.decodeStream(imageStream)

        var stream: ByteArrayOutputStream? = ByteArrayOutputStream()
        bmp.compress(Bitmap.CompressFormat.JPEG, 0, stream)
        bmp.recycle()
        val compressed:ByteArray? =(stream?.toByteArray())

        try {
            stream?.close()

        } catch (e: IOException) {
            s.onError(e)
        }

        s.onNext(compressed!!)
        s.onComplete()

    }.subscribeOn(Schedulers.computation())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeBy (
                    onNext = { compress -> callback.onNext(compress) },
                    onError =  { error-> callback.onError(error.message) },
                    onComplete = { callback.onCompleted() }
            )


}

这会失去正确的方向。事实上,当我尝试使用以下代码加载我给出的 byteArray 时:

  if(user.getPicUrl() != null) {
        Glide.with(this).load(user.getPicture()).bitmapTransform(new CenterCrop(context), new RoundCornerTransformation(context, 15, 2)).into(photo);
        PICTURE_INSERTED = true;
    }

图片旋转了 90 度。 我读过 Glide 支持 Exif 旋转,但在这种情况下它不起作用。为什么?有一种方法可以“存储”正确的方向以保存在服务器中吗?谢谢

【问题讨论】:

    标签: java android bitmap kotlin android-glide


    【解决方案1】:

    您必须将原始图像中的 EXIF 数据复制到最终调整大小的图像中。您可以使用 ExifInterface。

    https://developer.android.com/reference/android/media/ExifInterface.html#ExifInterface

    例如;

    val oldExif = ExifInterface(oldImagePath)
    val exifOrientation = oldExif.getAttribute(ExifInterface.TAG_ORIENTATION)
    
    if (exifOrientation != null) {
     val newExif = ExifInterface(imagePath)
     newExif.setAttribute(ExifInterface.TAG_ORIENTATION, exifOrientation)
     newExif.saveAttributes()
    }
    

    【讨论】:

    • github.com/zetbaitsu/Compressor 也许这个库对你有帮助。它的 min sdk 14。
    • 我现在正在查看 ExifInterface。它甚至支持 API 5。可能你的导入是错误的。我的导入是这样的:import android.media.ExifInterface
    猜你喜欢
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多