Skip to content

Instantly share code, notes, and snippets.

View liberaid2's full-sized avatar
🤷‍♂️
Android Developer

Egor Sheetov liberaid2

🤷‍♂️
Android Developer
  • St Petersburg
View GitHub Profile
@liberaid2
liberaid2 / CustomCameraBridge.kt
Created December 7, 2019 04:18
Custom bridge between camera and OpenCV for Android. This approach uses Camera2 API. Also there is a snippet for handling wrong camera rotation.
import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.ImageFormat
import android.hardware.camera2.CameraCaptureSession
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CameraDevice
import android.hardware.camera2.CameraManager
import android.media.ImageReader
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
fun CoroutineScope.launchUI(block: suspend CoroutineScope.() -> Unit) = this.launch(Dispatchers.Main, block = block)
suspend fun <T> CoroutineScope.withUI(block: suspend CoroutineScope.() -> T) = withContext(Dispatchers.Main, block = block)
fun CoroutineScope.launchCatching(block: suspend CoroutineScope.() -> Unit, onError: (ctx: CoroutineContext, throwable: Throwable) -> Unit) = launch(CoroutineExceptionHandler(onError), block = block)
fun CoroutineScope.launchUICatching(block: suspend CoroutineScope.() -> Unit, onError: (ctx: CoroutineContext, throwable: Throwable) -> Unit) = launch(CoroutineExceptionHandler(onError) + Dispatchers.Main, block = block)
@liberaid2
liberaid2 / TaskPool.kt
Created March 24, 2019 16:32
Coroutines Pool for task handling
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.selects.select
class TaskPool<T, R> (private val taskChannel: ReceiveChannel<T>, private val taskHandler: suspend CoroutineScope.(T) -> R, poolCapacity: Int = 5) {
private val resultChannel = Channel<R>(poolCapacity)
@liberaid2
liberaid2 / Makefile
Last active November 14, 2018 11:56
Common makefile
CC=g++
CFLAGS=-c -Wall -std=c++17 -g -O0
LDFLAGS=
LIBS=
SRCDIR=src
OBJDIR=obj
SOURCES=$(wildcard $(SRCDIR)/*.cpp)
OBJECTS=$(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SOURCES))
@liberaid2
liberaid2 / RVAdapter.kt
Last active October 19, 2018 23:03
RecyclerView adapter template for MVP pattern
package com.liberaid.mvprecyclerview.recyclerview
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
/**
* RecyclerView adapter template for MVP pattern
@liberaid2
liberaid2 / Makefile
Last active July 30, 2018 11:51
stm32f103c8 universal makefile (C/CPP)
TARGET = stm32template
DEBUG = 1
OPT = -Og
BUILD_DIR = build
C_SOURCES = $(shell find * -type f -name "*.c" | sed ':a;N;$!ba;s/\n/ /g')
CXX_SOURCES = $(shell find * -type f -name "*.cpp" | sed ':a;N;$!ba;s/\n/ /g')
ASM_SOURCES = startup_stm32f103xb.s