-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert
Converters.java
and HistoryItem.java
to kotlin
- Loading branch information
Showing
4 changed files
with
164 additions
and
237 deletions.
There are no files selected for viewing
79 changes: 0 additions & 79 deletions
79
app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/Converters.java
This file was deleted.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/Converters.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.secuso.privacyfriendlycodescanner.qrscanner.database | ||
|
||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import android.graphics.BitmapFactory | ||
import android.util.Base64 | ||
import androidx.room.TypeConverter | ||
import com.google.gson.Gson | ||
import com.google.zxing.BarcodeFormat | ||
import com.google.zxing.ResultPoint | ||
import java.io.ByteArrayOutputStream | ||
|
||
/** | ||
* This class offers type converters for the room database. | ||
* | ||
* @author Christopher Beckmann | ||
* @see AppDatabase | ||
*/ | ||
object Converters { | ||
@JvmStatic | ||
@TypeConverter | ||
fun encodeImage(bitmap: Bitmap?): String? { | ||
if (bitmap == null) return null | ||
val byteArrayOutputStream = ByteArrayOutputStream() | ||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream) | ||
val byteArray = byteArrayOutputStream.toByteArray() | ||
return Base64.encodeToString(byteArray, Base64.NO_WRAP) | ||
} | ||
|
||
@JvmStatic | ||
@TypeConverter | ||
fun decodeImage(encodedImage: String?): Bitmap? { | ||
if (encodedImage == null) return null | ||
val decodedBytes = Base64.decode(encodedImage, Base64.NO_WRAP) | ||
return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.size) | ||
} | ||
|
||
@JvmStatic | ||
@TypeConverter | ||
fun fromText(text: String?): BarcodeFormat? { | ||
return try { | ||
BarcodeFormat.valueOf(text!!) | ||
} catch (e: IllegalArgumentException) { | ||
null | ||
} catch (e: NullPointerException) { | ||
null | ||
} | ||
} | ||
|
||
@JvmStatic | ||
@TypeConverter | ||
fun fromBarcodeFormat(bcf: BarcodeFormat): String { | ||
return bcf.name | ||
} | ||
|
||
@JvmStatic | ||
@TypeConverter | ||
fun fromResultPoints(rp: Array<ResultPoint?>?): String? { | ||
val gson = Gson() | ||
return gson.toJson(rp) | ||
} | ||
|
||
@JvmStatic | ||
@TypeConverter | ||
fun convertResultPointFromJson(jsonString: String?): Array<ResultPoint?>? { | ||
val gson = Gson() | ||
return gson.fromJson(jsonString, Array<ResultPoint?>::class.java) | ||
} | ||
|
||
fun dpFromPx(context: Context, px: Float): Float { | ||
return px / context.resources.displayMetrics.density | ||
} | ||
|
||
fun pxFromDp(context: Context, dp: Float): Float { | ||
return dp * context.resources.displayMetrics.density | ||
} | ||
} |
158 changes: 0 additions & 158 deletions
158
app/src/main/java/com/secuso/privacyfriendlycodescanner/qrscanner/database/HistoryItem.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.