ImageUtil
Class Initialize
การเริ่มต้นใช้งาน จะมี 2 กรณีคือ companion object function และ function ที่จะต้องประกาศ object ของคลาสตามตัวอย่าง
class ImageUtil(context: Context)
Variables Companion Object
| Name | Description | Type | Default | Variable Type |
|---|
| EXT_JPEG | นามสกุลไฟล์ .jpg | String | “.jpg” | const val |
| EXT_PNG | นามสกุลไฟล์ .png | String | “.png” | const val |
| REQUEST_CODE_PHOTOCAM | Request Code 10002 ไว้ใช้สำหรับเช็คใน onActivityResult | Int | 10002 | const val |
Method Companion Object Overview
| Name | Description | Return |
|---|
| encodeBitmapTobase64() | เข้ารหัส Bitmap ให้เป็น Base64 | String? |
| decodeToBase64() | ถอดรหัส String เป็น Bitmap | Bitmap? |
| convertUriToBitmap() | แปลง Uri เป็น Bitmap | Bitmap |
| convertURLtoBitmap() | แปลง URL เป็น Bitmap | Bitmap |
| getResizedBitmap() | แปลงขนาด Bitmap | Bitmap |
| getAngleImage() | ค่ามุมของรูป | Int |
| rotateImage() | หมุน Bitmap | Bitmap |
Method Companion Object Details
fun encodeBitmapTobase64(image: Bitmap? , ext : String) : String?
เข้ารหัส Bitmap ให้เป็น Base64
Parameters
| Name | Description | Type |
|---|
| image | Bitmap ของรูปที่ต้องการ | Bitmap? |
| ext | นามสกุลของไฟล์รูป คือ EXT_JPEG หรือ EXT_JPEG | String |
Returns
| Type | Description |
|---|
| String? | Return String ที่เข้ารหัส Base64 |
fun decodeToBase64(input: String?): Bitmap?
ถอดรหัส String เป็น Bitmap
Parameters
| Name | Description | Type |
|---|
| input | String ที่ต้องการถอดรหัส | String? |
Returns
| Type | Description |
|---|
| Bitmap? | Return Bitmap ที่ถอดจาก String |
fun convertUriToBitmap(uri : Uri) : Bitmap
แปลง Uri เป็น Bitmap
Parameters
| Name | Description | Type |
|---|
| uri | uri ที่ต้องการแปลง | Uri |
Returns
| Type | Description |
|---|
| Bitmap | Return Bitmap ที่แปลงจาก Uri |
fun convertURLtoBitmap(url: URL) : Bitmap
แปลง URL เป็น Bitmap
Parameters
| Name | Description | Type |
|---|
| url | url ที่ต้องการแปลง | URL |
Returns
| Type | Description |
|---|
| Bitmap | Return Bitmap ที่แปลงจาก URL |
fun getResizedBitmap(path: String , image: Bitmap , maxWidth : Int , maxHeight : Int) : Bitmap
แปลงขนาด Bitmap
Parameters
| Name | Description | Type |
|---|
| path | path ของรูปที่ต้องการ | String |
| image | Bitmap ที่เป็นรูปที่ต้องการ | Bitmap |
| maxWidth | ความกว้างสูงสุดที่ต้องการ | Int |
| maxHeight | ความสูงสูงสุดที่ต้องการ | Int |
Returns
| Type | Description |
|---|
| Bitmap | Return Bitmap ที่เปลี่ยนแปลงขนาดแล้ว |
fun getAngleImage(photoPath: String): Int
ค่ามุมของรูป
Parameters
| Name | Description | Type |
|---|
| photoPath | path ไฟล์รูป | String |
Returns
| Type | Description |
|---|
| Int | Return Int ที่เป็นค่ามุมของรูป |
fun rotateImage(bitmap : Bitmap, angle: Float): Bitmap
หมุน Bitmap
Parameters
| Name | Description | Type |
|---|
| bitmap | Bitmap ที่ต้องการหมุน | Bitmap |
| angle | มุมที่ต้องการหมุน | Float |
Returns
| Type | Description |
|---|
| Bitmap | Return Bitmap ที่หมุนแล้ว |
Method class Overview
| Name | Description | Return |
|---|
| addBitmapToCacheDir() | บันทึก Bitmap ไปที่ cache | String |
Method Details
fun addBitmapToCacheDir(bitmap: Bitmap , imageName: String = DateUtil.getTimestamp().toString(), ext: String = EXT_JPEG): String
บันทึก Bitmap ไปที่ cache
Parameters
| Name | Description | Type | Default |
|---|
| bitmap | bitmap ของรูป | Bitmap | |
| imageName | ชื่อไฟล์รูป | String | DateUtil.getTimestamp().toString() |
| ext | นามสกุลไฟล์ | String | EXT_JPEG |
Returns
| Type | Description |
|---|
| String | Return String เป็น path ที่บันทึก |