add asyncTask for converting images
This commit is contained in:
parent
67f9b52b66
commit
00c160a015
6 changed files with 126 additions and 70 deletions
|
@ -11,6 +11,8 @@ import net.osmand.telegram.helpers.TelegramUiHelper.ListItem
|
||||||
import net.osmand.telegram.utils.AndroidUtils
|
import net.osmand.telegram.utils.AndroidUtils
|
||||||
import org.drinkless.td.libcore.telegram.TdApi
|
import org.drinkless.td.libcore.telegram.TdApi
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import android.net.Uri
|
||||||
|
import net.osmand.telegram.R
|
||||||
|
|
||||||
class ShowLocationHelper(private val app: TelegramApplication) {
|
class ShowLocationHelper(private val app: TelegramApplication) {
|
||||||
|
|
||||||
|
@ -30,7 +32,7 @@ class ShowLocationHelper(private val app: TelegramApplication) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun showLocationOnMap(item: ListItem) {
|
fun showLocationOnMap(item: ListItem, stale: Boolean = false) {
|
||||||
if (item.latLon == null) {
|
if (item.latLon == null) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -44,7 +46,7 @@ class ShowLocationHelper(private val app: TelegramApplication) {
|
||||||
Color.WHITE,
|
Color.WHITE,
|
||||||
ALatLon(item.latLon!!.latitude, item.latLon!!.longitude),
|
ALatLon(item.latLon!!.latitude, item.latLon!!.longitude),
|
||||||
null,
|
null,
|
||||||
generatePhotoParams(item.photoPath)
|
generatePhotoParams(if (stale) item.greyScaledPhotoPath else item.photoPath, stale)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,11 +55,11 @@ class ShowLocationHelper(private val app: TelegramApplication) {
|
||||||
execOsmandApi {
|
execOsmandApi {
|
||||||
val messages = telegramHelper.getMessages()
|
val messages = telegramHelper.getMessages()
|
||||||
for (message in messages) {
|
for (message in messages) {
|
||||||
val date = telegramHelper.getLastUpdatedTime(message) * 1000L
|
val date = telegramHelper.getLastUpdatedTime(message)
|
||||||
val messageShowingTime = System.currentTimeMillis() - date
|
val messageShowingTime = System.currentTimeMillis() / 1000 - date
|
||||||
if (messageShowingTime > app.settings.locHistoryTime * 1000L) {
|
if (messageShowingTime > app.settings.locHistoryTime) {
|
||||||
removeMapPoint(message.chatId, message)
|
removeMapPoint(message.chatId, message)
|
||||||
} else {
|
} else if (app.settings.isShowingChatOnMap(message.chatId)) {
|
||||||
addOrUpdateLocationOnMap(message, true)
|
addOrUpdateLocationOnMap(message, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,6 +74,8 @@ class ShowLocationHelper(private val app: TelegramApplication) {
|
||||||
if (chatTitle != null && content is TdApi.MessageLocation) {
|
if (chatTitle != null && content is TdApi.MessageLocation) {
|
||||||
var userName = ""
|
var userName = ""
|
||||||
var photoPath: String? = null
|
var photoPath: String? = null
|
||||||
|
val date = telegramHelper.getLastUpdatedTime(message) * 1000L
|
||||||
|
val stale = System.currentTimeMillis() - date > app.settings.staleLocTime * 1000L
|
||||||
val user = telegramHelper.getUser(message.senderUserId)
|
val user = telegramHelper.getUser(message.senderUserId)
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
userName = "${user.firstName} ${user.lastName}".trim()
|
userName = "${user.firstName} ${user.lastName}".trim()
|
||||||
|
@ -81,9 +85,7 @@ class ShowLocationHelper(private val app: TelegramApplication) {
|
||||||
if (userName.isEmpty()) {
|
if (userName.isEmpty()) {
|
||||||
userName = user.phoneNumber
|
userName = user.phoneNumber
|
||||||
}
|
}
|
||||||
val date = telegramHelper.getLastUpdatedTime(message) * 1000L
|
photoPath = if (stale) {
|
||||||
val expired = System.currentTimeMillis() - date > app.settings.staleLocTime * 1000L
|
|
||||||
photoPath = if (expired) {
|
|
||||||
telegramHelper.getUserGreyPhotoPath(user)
|
telegramHelper.getUserGreyPhotoPath(user)
|
||||||
} else {
|
} else {
|
||||||
telegramHelper.getUserPhotoPath(user)
|
telegramHelper.getUserPhotoPath(user)
|
||||||
|
@ -93,7 +95,7 @@ class ShowLocationHelper(private val app: TelegramApplication) {
|
||||||
userName = message.senderUserId.toString()
|
userName = message.senderUserId.toString()
|
||||||
}
|
}
|
||||||
setupMapLayer()
|
setupMapLayer()
|
||||||
val params = generatePhotoParams(photoPath)
|
val params = generatePhotoParams(photoPath, stale)
|
||||||
if (update) {
|
if (update) {
|
||||||
osmandAidlHelper.updateMapPoint(MAP_LAYER_ID, "${chatId}_${message.senderUserId}", userName, userName,
|
osmandAidlHelper.updateMapPoint(MAP_LAYER_ID, "${chatId}_${message.senderUserId}", userName, userName,
|
||||||
chatTitle, Color.WHITE, ALatLon(content.location.latitude, content.location.longitude), null, params)
|
chatTitle, Color.WHITE, ALatLon(content.location.latitude, content.location.longitude), null, params)
|
||||||
|
@ -153,11 +155,20 @@ class ShowLocationHelper(private val app: TelegramApplication) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generatePhotoParams(photoPath: String?): Map<String, String>? {
|
private fun generatePhotoParams(photoPath: String?, stale: Boolean = false): Map<String, String>? {
|
||||||
if (TextUtils.isEmpty(photoPath)) {
|
val photoUri = if (TextUtils.isEmpty(photoPath)) {
|
||||||
return null
|
if (stale) {
|
||||||
|
AndroidUtils.resourceToUri(app, R.drawable.img_user_picture)
|
||||||
|
} else {
|
||||||
|
AndroidUtils.resourceToUri(app, R.drawable.img_user_picture_active)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
AndroidUtils.getUriForFile(app, File(photoPath))
|
||||||
}
|
}
|
||||||
val photoUri = AndroidUtils.getUriForFile(app, File(photoPath))
|
return generatePhotoParamsFromUri(photoUri)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun generatePhotoParamsFromUri(photoUri: Uri): Map<String, String>? {
|
||||||
app.grantUriPermission(
|
app.grantUriPermission(
|
||||||
app.settings.appToConnectPackage,
|
app.settings.appToConnectPackage,
|
||||||
photoUri,
|
photoUri,
|
||||||
|
|
|
@ -311,10 +311,10 @@ class TelegramHelper private constructor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getUserGreyPhotoPath(user: TdApi.User): String? {
|
fun getUserGreyPhotoPath(user: TdApi.User): String? {
|
||||||
return if (hasLocalGreyUserPhoto(user.id)) {
|
return if (hasGrayscaleUserPhoto(user.id)) {
|
||||||
"$appDir/$PROFILE_GREY_PHOTOS_DIR${user.id}$SAVED_GREY_PHOTOS_EXT"
|
"$appDir/$PROFILE_GRAYSCALE_PHOTOS_DIR${user.id}$SAVED_GRAYSCALE_PHOTOS_EXT"
|
||||||
} else {
|
} else {
|
||||||
getUserPhotoPath(user)
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -356,8 +356,8 @@ class TelegramHelper private constructor() {
|
||||||
updateLiveMessagesExecutor?.awaitTermination(1, TimeUnit.MINUTES)
|
updateLiveMessagesExecutor?.awaitTermination(1, TimeUnit.MINUTES)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun hasLocalGreyUserPhoto(userId: Int): Boolean {
|
fun hasGrayscaleUserPhoto(userId: Int): Boolean {
|
||||||
return File("$appDir/$PROFILE_GREY_PHOTOS_DIR$userId$SAVED_GREY_PHOTOS_EXT").exists()
|
return File("$appDir/$PROFILE_GRAYSCALE_PHOTOS_DIR$userId$SAVED_GRAYSCALE_PHOTOS_EXT").exists()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun hasLocalUserPhoto(user: TdApi.User): Boolean {
|
private fun hasLocalUserPhoto(user: TdApi.User): Boolean {
|
||||||
|
|
|
@ -144,7 +144,6 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
|
||||||
override fun onSendLiveLocationError(code: Int, message: String) {}
|
override fun onSendLiveLocationError(code: Int, message: String) {}
|
||||||
|
|
||||||
override fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) {
|
override fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) {
|
||||||
app.uiUtils.checkUserPhotoFromChat(chatId)
|
|
||||||
app.runInUIThread { updateList() }
|
app.runInUIThread { updateList() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -313,8 +312,12 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
|
||||||
val openOnMapView = holder.getOpenOnMapClickView()
|
val openOnMapView = holder.getOpenOnMapClickView()
|
||||||
|
|
||||||
val staleLocation = System.currentTimeMillis() / 1000 - item.lastUpdated > settings.staleLocTime
|
val staleLocation = System.currentTimeMillis() / 1000 - item.lastUpdated > settings.staleLocTime
|
||||||
TelegramUiHelper.setupPhoto(app, holder.icon, if (staleLocation) item.greyScaledPhotoPath else item.photoPath,
|
if (staleLocation) {
|
||||||
item.placeholderId, false)
|
TelegramUiHelper.setupPhoto(app, holder.icon, item.greyScaledPhotoPath, item.placeholderId, false)
|
||||||
|
} else {
|
||||||
|
TelegramUiHelper.setupPhoto(app, holder.icon, item.photoPath, R.drawable.img_user_picture_active, false)
|
||||||
|
}
|
||||||
|
|
||||||
holder.title?.text = item.getVisibleName()
|
holder.title?.text = item.getVisibleName()
|
||||||
openOnMapView?.isEnabled = canBeOpenedOnMap
|
openOnMapView?.isEnabled = canBeOpenedOnMap
|
||||||
if (canBeOpenedOnMap) {
|
if (canBeOpenedOnMap) {
|
||||||
|
@ -322,7 +325,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
|
||||||
if (!isOsmAndInstalled()) {
|
if (!isOsmAndInstalled()) {
|
||||||
showOsmAndMissingDialog()
|
showOsmAndMissingDialog()
|
||||||
} else {
|
} else {
|
||||||
app.showLocationHelper.showLocationOnMap(item)
|
app.showLocationHelper.showLocationOnMap(item, staleLocation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -32,7 +32,7 @@ private const val PERMISSION_REQUEST_LOCATION = 1
|
||||||
private const val MY_LOCATION_TAB_POS = 0
|
private const val MY_LOCATION_TAB_POS = 0
|
||||||
private const val LIVE_NOW_TAB_POS = 1
|
private const val LIVE_NOW_TAB_POS = 1
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListener {
|
class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListener, TelegramIncomingMessagesListener {
|
||||||
|
|
||||||
private val log = PlatformUtil.getLog(MainActivity::class.java)
|
private val log = PlatformUtil.getLog(MainActivity::class.java)
|
||||||
|
|
||||||
|
@ -143,6 +143,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
|
||||||
if (telegramHelper.listener != this) {
|
if (telegramHelper.listener != this) {
|
||||||
telegramHelper.listener = this
|
telegramHelper.listener = this
|
||||||
}
|
}
|
||||||
|
telegramHelper.addIncomingMessagesListener(this)
|
||||||
|
|
||||||
app.locationProvider.checkIfLastKnownLocationIsValid()
|
app.locationProvider.checkIfLastKnownLocationIsValid()
|
||||||
|
|
||||||
|
@ -159,6 +160,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
|
||||||
override fun onPause() {
|
override fun onPause() {
|
||||||
super.onPause()
|
super.onPause()
|
||||||
telegramHelper.listener = null
|
telegramHelper.listener = null
|
||||||
|
telegramHelper.removeIncomingMessagesListener(this)
|
||||||
|
|
||||||
app.locationProvider.pauseAllUpdates()
|
app.locationProvider.pauseAllUpdates()
|
||||||
|
|
||||||
|
@ -207,18 +209,23 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onTelegramChatsChanged() {
|
override fun onTelegramChatsChanged() {
|
||||||
|
telegramHelper.getMessagesByChatIds().forEach {
|
||||||
|
app.uiUtils.checkUserPhotoFromChat(it.key)
|
||||||
|
}
|
||||||
runOnUi {
|
runOnUi {
|
||||||
listeners.forEach { it.get()?.onTelegramChatsChanged() }
|
listeners.forEach { it.get()?.onTelegramChatsChanged() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onTelegramChatChanged(chat: TdApi.Chat) {
|
override fun onTelegramChatChanged(chat: TdApi.Chat) {
|
||||||
|
app.uiUtils.checkUserPhotoFromChat(chat.id)
|
||||||
runOnUi {
|
runOnUi {
|
||||||
listeners.forEach { it.get()?.onTelegramChatChanged(chat) }
|
listeners.forEach { it.get()?.onTelegramChatChanged(chat) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onTelegramUserChanged(user: TdApi.User) {
|
override fun onTelegramUserChanged(user: TdApi.User) {
|
||||||
|
app.uiUtils.checkUserGreyPhoto(user.id, telegramHelper.getUserPhotoPath(user))
|
||||||
val message = telegramHelper.getUserMessage(user)
|
val message = telegramHelper.getUserMessage(user)
|
||||||
if (message != null) {
|
if (message != null) {
|
||||||
app.showLocationHelper.addOrUpdateLocationOnMap(message)
|
app.showLocationHelper.addOrUpdateLocationOnMap(message)
|
||||||
|
@ -243,6 +250,14 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) {
|
||||||
|
app.uiUtils.checkUserPhotoFromChat(chatId)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDeleteChatLocationMessages(chatId: Long, messages: List<TdApi.Message>) {}
|
||||||
|
|
||||||
|
override fun updateLocationMessages() {}
|
||||||
|
|
||||||
override fun switchButtonsVisibility(visible: Boolean) {
|
override fun switchButtonsVisibility(visible: Boolean) {
|
||||||
val buttonsVisibility = if (visible) View.VISIBLE else View.GONE
|
val buttonsVisibility = if (visible) View.VISIBLE else View.GONE
|
||||||
if (buttonsBar.visibility != buttonsVisibility) {
|
if (buttonsBar.visibility != buttonsVisibility) {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package net.osmand.telegram.utils
|
||||||
|
|
||||||
import android.Manifest
|
import android.Manifest
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
|
import android.content.ContentResolver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
|
@ -113,6 +114,14 @@ object AndroidUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun resourceToUri(ctx: Context, resID: Int): Uri {
|
||||||
|
return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
|
||||||
|
"://${ctx.resources.getResourcePackageName(resID)}" +
|
||||||
|
"/${ctx.resources.getResourceTypeName(resID)}" +
|
||||||
|
"/${ctx.resources.getResourceEntryName(resID)}"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fun isAppInstalled(ctx: Context, appPackage: String): Boolean {
|
fun isAppInstalled(ctx: Context, appPackage: String): Boolean {
|
||||||
try {
|
try {
|
||||||
ctx.packageManager.getPackageInfo(appPackage, 0)
|
ctx.packageManager.getPackageInfo(appPackage, 0)
|
||||||
|
|
|
@ -6,6 +6,7 @@ import android.graphics.drawable.Drawable
|
||||||
import android.graphics.drawable.LayerDrawable
|
import android.graphics.drawable.LayerDrawable
|
||||||
import android.hardware.Sensor
|
import android.hardware.Sensor
|
||||||
import android.hardware.SensorManager
|
import android.hardware.SensorManager
|
||||||
|
import android.os.AsyncTask
|
||||||
import android.support.annotation.ColorInt
|
import android.support.annotation.ColorInt
|
||||||
import android.support.annotation.ColorRes
|
import android.support.annotation.ColorRes
|
||||||
import android.support.annotation.DrawableRes
|
import android.support.annotation.DrawableRes
|
||||||
|
@ -26,14 +27,12 @@ import java.io.FileOutputStream
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
const val PROFILE_GREY_PHOTOS_DIR = "profile_grey_photos/"
|
const val PROFILE_GRAYSCALE_PHOTOS_DIR = "profile_grayscale_photos/"
|
||||||
|
|
||||||
const val SAVED_GREY_PHOTOS_EXT = ".png"
|
const val SAVED_GRAYSCALE_PHOTOS_EXT = ".jpeg"
|
||||||
|
|
||||||
class UiUtils(private val app: TelegramApplication) {
|
class UiUtils(private val app: TelegramApplication) {
|
||||||
|
|
||||||
private val log = PlatformUtil.getLog(UiUtils::class.java)
|
|
||||||
|
|
||||||
private val drawableCache = LinkedHashMap<Long, Drawable>()
|
private val drawableCache = LinkedHashMap<Long, Drawable>()
|
||||||
private val circleBitmapCache = LinkedHashMap<String, Bitmap>()
|
private val circleBitmapCache = LinkedHashMap<String, Bitmap>()
|
||||||
|
|
||||||
|
@ -121,51 +120,17 @@ class UiUtils(private val app: TelegramApplication) {
|
||||||
val chat = app.telegramHelper.getChat(chatId)
|
val chat = app.telegramHelper.getChat(chatId)
|
||||||
val chatIconPath = chat?.photo?.small?.local?.path
|
val chatIconPath = chat?.photo?.small?.local?.path
|
||||||
if (chat != null && chatIconPath != null) {
|
if (chat != null && chatIconPath != null) {
|
||||||
val userId = app.telegramHelper.getUserIdFromChatType(chat.type)
|
checkUserGreyPhoto(app.telegramHelper.getUserIdFromChatType(chat.type), chatIconPath)
|
||||||
if (userId != 0 && !app.telegramHelper.hasLocalGreyUserPhoto(userId)) {
|
|
||||||
convertToGrayscaleAndSave(
|
|
||||||
chatIconPath,
|
|
||||||
"${app.filesDir.absolutePath}/$PROFILE_GREY_PHOTOS_DIR$userId$SAVED_GREY_PHOTOS_EXT"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun convertToGrayscaleAndSave(coloredImagePath: String, newFilePath: String) {
|
|
||||||
val currentImage = BitmapFactory.decodeFile(coloredImagePath)
|
|
||||||
val greyedImage = toGrayscale(currentImage)
|
|
||||||
saveBitmap(greyedImage, newFilePath)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun toGrayscale(bmpOriginal: Bitmap): Bitmap {
|
fun checkUserGreyPhoto(userId: Int, userOriginalPhotoPath: String?) {
|
||||||
val bmpGrayscale = Bitmap.createBitmap(bmpOriginal.width, bmpOriginal.height, Bitmap.Config.ARGB_8888)
|
if (userId != 0 && !app.telegramHelper.hasGrayscaleUserPhoto(userId)) {
|
||||||
val c = Canvas(bmpGrayscale)
|
ConvertPhotoToGrayscale().executeOnExecutor(
|
||||||
val paint = Paint()
|
AsyncTask.THREAD_POOL_EXECUTOR,
|
||||||
val cm = ColorMatrix()
|
userOriginalPhotoPath,
|
||||||
cm.setSaturation(0f)
|
"${app.filesDir.absolutePath}/$PROFILE_GRAYSCALE_PHOTOS_DIR$userId$SAVED_GRAYSCALE_PHOTOS_EXT"
|
||||||
val f = ColorMatrixColorFilter(cm)
|
)
|
||||||
paint.colorFilter = f
|
|
||||||
c.drawBitmap(bmpOriginal, 0f, 0f, paint)
|
|
||||||
return bmpGrayscale
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun saveBitmap(bitmap: Bitmap, newFilePath: String) {
|
|
||||||
var fout: FileOutputStream? = null
|
|
||||||
try {
|
|
||||||
val file = File(newFilePath)
|
|
||||||
if (file.parentFile != null) {
|
|
||||||
file.parentFile.mkdirs()
|
|
||||||
}
|
|
||||||
fout = FileOutputStream(file)
|
|
||||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fout)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
log.error(e)
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
fout?.close()
|
|
||||||
} catch (e: IOException) {
|
|
||||||
log.error(e)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -270,4 +235,57 @@ class UiUtils(private val app: TelegramApplication) {
|
||||||
var screenOrientation: Int = 0
|
var screenOrientation: Int = 0
|
||||||
var outdatedLocation: Boolean = false
|
var outdatedLocation: Boolean = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class ConvertPhotoToGrayscale : AsyncTask<String, String, Void?>() {
|
||||||
|
|
||||||
|
private val log = PlatformUtil.getLog(ConvertPhotoToGrayscale::class.java)
|
||||||
|
|
||||||
|
override fun doInBackground(vararg params: String?): Void? {
|
||||||
|
val userOriginalPhotoPath = params[0]
|
||||||
|
val userGrayScalePhotoPath = params[1]
|
||||||
|
if (userOriginalPhotoPath != null && userGrayScalePhotoPath != null) {
|
||||||
|
convertToGrayscaleAndSave(userOriginalPhotoPath, userGrayScalePhotoPath)
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
fun convertToGrayscaleAndSave(coloredImagePath: String, newFilePath: String) {
|
||||||
|
val currentImage = BitmapFactory.decodeFile(coloredImagePath)
|
||||||
|
val greyedImage = toGrayscale(currentImage)
|
||||||
|
saveBitmap(greyedImage, newFilePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun toGrayscale(bmpOriginal: Bitmap): Bitmap {
|
||||||
|
val bmpGrayscale =
|
||||||
|
Bitmap.createBitmap(bmpOriginal.width, bmpOriginal.height, Bitmap.Config.ARGB_8888)
|
||||||
|
val c = Canvas(bmpGrayscale)
|
||||||
|
val paint = Paint()
|
||||||
|
val cm = ColorMatrix()
|
||||||
|
cm.setSaturation(0f)
|
||||||
|
val f = ColorMatrixColorFilter(cm)
|
||||||
|
paint.colorFilter = f
|
||||||
|
c.drawBitmap(bmpOriginal, 0f, 0f, paint)
|
||||||
|
return bmpGrayscale
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun saveBitmap(bitmap: Bitmap, newFilePath: String) {
|
||||||
|
var fout: FileOutputStream? = null
|
||||||
|
try {
|
||||||
|
val file = File(newFilePath)
|
||||||
|
if (file.parentFile != null) {
|
||||||
|
file.parentFile.mkdirs()
|
||||||
|
}
|
||||||
|
fout = FileOutputStream(file)
|
||||||
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
log.error(e)
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
fout?.close()
|
||||||
|
} catch (e: IOException) {
|
||||||
|
log.error(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue