Skip to content

Instantly share code, notes, and snippets.

View LiewJunTung's full-sized avatar

Liew Jun Tung LiewJunTung

View GitHub Profile
@LiewJunTung
LiewJunTung / build.gradle.kts
Created July 29, 2019 16:24
Gradle plugin tutorial
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.3.41"
`java-gradle-plugin`
id("com.gradle.plugin-publish") version "0.10.0"
}
pluginBundle {
website = "<substitute your project website>"
abstract class SymbolicLinkTask : DefaultTask() {
var actualFilePath: String? = null
var symbolicDirPath: String? = null
var rename: String? = null
@TaskAction
fun link() {
val aPath = actualFilePath ?: throw Exception("actualPath not set")
val sPath = symbolicDirPath ?: throw Exception("symbolicPath not set")
@LiewJunTung
LiewJunTung / RgbConversion.kt
Created July 13, 2019 18:21
Best solution YUV -> RGB
class RgbConversion(val rs: RenderScript, private val feedSize: Size, private val hasRotate: Boolean = true) {
private var mInputAllocation: Allocation? = null
private var mOutputAllocation: Allocation? = null
private var mRotatedAllocation: Allocation? = null
private val yuvToRgb = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs))
private val rotator = ScriptC_rotator(rs)
var bufferCallback: ((ByteBuffer) -> Unit)? = null
val inputSurface: Surface
get() = mInputAllocation!!.surface
fun rotateBitmap(context: Context, bitmap: Bitmap): Bitmap{
val rs = RenderScript.create(context)
val allocation = Allocation.createFromBitmap(rs, bitmap)
val rotated = Allocation.createTyped(rs, Type.createXY(rs, Element.RGBA_8888(rs), bitmap.height, bitmap.width))
ScriptC_rotator(rs).apply {
_inImage = allocation
_inHeight = bitmap.height
_inWidth = bitmap.width
forEach_rotate_270_clockwise(rotated, rotated)
package com.liewjuntung.cameraxfacetracking
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.os.Bundle
import android.os.Handler
import android.os.HandlerThread
import android.renderscript.RenderScript
import android.util.Rational
import android.util.Size
class RsYuvToRgb(val rs: RenderScript) {
private var rotateAllocation: Allocation? = null
private var mOutputAllocation: Allocation? = null
private var mInputAllocation: Allocation? = null
private val rsYuvToRgb = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs))
private var yArray: ByteArray? = null
private var uArray: ByteArray? = null
@LiewJunTung
LiewJunTung / rotator.rs
Created July 4, 2019 18:57
RenderScript to rotate image buffer after convert YUV to RGB
#pragma version(1)
#pragma rs java_package_name(com.liewjuntung.image)
rs_allocation inImage;
int inWidth;
int inHeight;
float rotation = 0.0f;
@LiewJunTung
LiewJunTung / YuvToRgb.kt
Last active July 9, 2019 17:27
Best performance
class RsYuvToRgb(private val rs: RenderScript) {
private var mOutputAllocation: Allocation? = null
private var mInputAllocation: Allocation? = null
private val rsYuvToRgb = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs))
private var yArray: ByteArray? = null
private var uArray: ByteArray? = null
private var vArray: ByteArray? = null
@LiewJunTung
LiewJunTung / RgbConversion.kt
Last active July 1, 2019 02:38
CameraX ImageAnalysis Use Case with RenderScript. Look at line 309-312 in XImageAnalysis.kt
package org.netvirta.thecamerax
import android.graphics.Bitmap
import android.graphics.ImageFormat
import android.renderscript.Allocation
import android.renderscript.Element
import android.renderscript.RenderScript
import android.renderscript.ScriptIntrinsicYuvToRGB
import android.renderscript.Type
import android.util.Log