مدیریت کاربران مشترک
بروز خطا
امتیازات دریافتی
حساب
آموزش هوش مصنوعی برای برنامه نویسان
وبینار ها
package com.example.serversidebyasp.Class
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.util.Base64
import java.io.ByteArrayOutputStream
class ImageGallery {
fun getBitmap(context: Context, uri: Uri): Bitmap {
val cursor = context.getContentResolver().query(uri, null, null, null, null)
cursor?.moveToFirst()
val filePath = cursor?.getString(1)
cursor?.close()
return BitmapFactory.decodeFile(filePath)
}
fun showImage(context: Context, REQUEST_CHOOSE_IMAGE: Int) {
val GalIntent = Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
(context as Activity).startActivityForResult(GalIntent, REQUEST_CHOOSE_IMAGE)
}
fun getStringImage(bitmp: Bitmap, IMAGE_MAX_SIZE: Int): String {
var bmp = bitmp
bmp = getResizedBitmap(bmp, IMAGE_MAX_SIZE)
val baos = ByteArrayOutputStream()
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos)
val imageBytes = baos.toByteArray()
return Base64.encodeToString(imageBytes, Base64.DEFAULT)
}
private fun getResizedBitmap(image: Bitmap, IMAGE_MAX_SIZE: Int): Bitmap {
var width = image.width
var height = image.height
val bitmapRatio = width.toFloat() / height.toFloat()
if (bitmapRatio > 1) {
width = IMAGE_MAX_SIZE
height = (width / bitmapRatio).toInt()
} else {
height = IMAGE_MAX_SIZE
width = (height * bitmapRatio).toInt()
}
return Bitmap.createScaledBitmap(image, width, height, true)
}
fun getBitmap(encodedImage: String): Bitmap {
val decodedString = Base64.decode(encodedImage, Base64.DEFAULT)
return BitmapFactory.decodeByteArray(decodedString, 0, decodedString.size)
}
}