Kotlin图像压缩实现 [英] Kotlin Image Compression Implementation
问题描述
我以前的实现是在不进行任何压缩的情况下将图像以JPEG格式上传到Firebase存储
My old implementation to upload image to Firebase Storage in JPEG format without any compression
private fun sendToFirebase() {
if (imgUri != null) {
val fileRef = storageRef!!.child(username+ ".jpg")
....
// code to upload and read image url
}
}
决定编写一种图像压缩技术来压缩图像,然后将其上传到Firebase存储中
结果:实现了图像压缩技术,请参见下文
Result : Achieved image compression technique, see below
新添加的用于压缩图像的代码
Newly added code to compress image
-
位图的URI
URI to Bitmap
val bitmap = MediaStore.Images.Media.getBitmap(activity?.contentResolver, imgUri)
压缩位图的方法
Method to compress Bitmap
private fun compressBitmap(bitmap: Bitmap, quality:Int):Bitmap{
val stream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.WEBP, quality, stream)
val byteArray = stream.toByteArray()
return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
}
位图压缩实现
Bitmap compression Implementation
compressBitmap(bitmap, 80)
查询:如何将相同的压缩图像上传到Firebase存储
Query: How to upload same compressed image to Firebase storage
private fun sendToFirebase() {
if (imgUri != null) {
// code to convert uri to bitmap <start>
val bitmap = MediaStore.Images.Media.getBitmap(activity?.contentResolver, imgUri)
compressBitmap(bitmap, 80)
// code to convert uri to bitmap <end>
// old implementation
.....
}
}
推荐答案
您似乎没有将任何内容传递给sendtoFirebase
的函数.我正在发布成功上传的代码.
You don't seem to be passing anything into your function for sendtoFirebase
. i am posting code i have done to successfully upload.
您首先考虑进行压缩,所以您将需要它;
you looking at compressing first so you would need this;
private fun compressBitmap(bitmap: Bitmap, quality: Int): Bitmap {
val stream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.WEBP,quality,stream)
val byteArray = stream.toByteArray()
arrayByte = byteArray
uploadFile(arrayByte)
return BitmapFactory.decodeByteArray(byteArray,0,byteArray.size)
}
在上面的
中,uploadFile
是Firebase上传的调用.我正在将压缩的位图传递给函数.上传功能如下:
in the above, uploadFile
is the call for the firebase upload. i am passing the compressed bitmap into the function. the functional for upload looks as follows:
是一个伴随对象,它是为压缩而传递的URI的一部分.如果您不想执行检查,则可以删除以下if语句
in below mImageURI
is a companion object which is part of the URI passed for compression. you can remove the if statement below if you dont want to do the check
private fun uploadFile(data:ByteArray) {
if (mImageUri != null){
val storageref = imageref.child("put your image id here")
storageref.putBytes(data).addOnSuccessListener {
Handler().postDelayed({
progressbar.setProgress(0)
Toast.makeText(activity, "Upload Successful", Toast.LENGTH_LONG).show()
}
, 1000)
}.addOnFailureListener{e->
Toast.makeText(activity,e.message,Toast.LENGTH_LONG).show()
}.addOnProgressListener {taskSnapshot ->
val progress = (100.0 * taskSnapshot.bytesTransferred/taskSnapshot.totalByteCount)
progressbar.setProgress(progress.toInt())
}
}
else if(mImageUri == null) {
Toast.makeText(activity,"No File Selected",Toast.LENGTH_LONG).show()
}
}
您不需要上面的进度栏.如果文件很大,它只是用户观看上传进度的好视觉效果.
You do not need to have the progress bar above. its just a nice visual for the user to have to see the progress of the upload if the file is large.
您实际上只需要确保将data
传递到.putbytes
your really only need to ensure that you passing data
into .putbytes
如果您的代码类似于我的代码,请使用onActivity结果;
For your onActivity result if your code is similar to mine then use;
重写乐趣onActivityResult(requestCode:Int,resultCode:Int,数据:Intent?){ super.onActivityResult(requestCode,resultCode,数据)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
&& data != null && data.getData() != null) {
mImageUri = data.getData()!!
image1.setImageURI(data.getData())
}
}
上图image1中的
是当前页面上的imageView,用于显示所选的图像.
in the above image1 is a imageView on the current page to show the image selected.
希望这会有所帮助
这篇关于Kotlin图像压缩实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!