Skip to content

Instantly share code, notes, and snippets.

View mebjas's full-sized avatar
🖥️
Working on a camera for everyone 🐝 #CameraGo

minhaz mebjas

🖥️
Working on a camera for everyone 🐝 #CameraGo
View GitHub Profile
@mebjas
mebjas / html-file.html
Last active March 24, 2024 15:43
Demo code on using github.com/mebjas/html5-qrcode. This was used in https://blog.minhazav.dev/research/html5-qrcode.html
<html>
<head>
<title>Html-Qrcode Demo</title>
<body>
<div id="qr-reader" style="width:500px"></div>
<div id="qr-reader-results"></div>
</body>
<script src="https://raw.githubusercontent.com/mebjas/html5-qrcode/master/minified/html5-qrcode.min.js"></script>
<script src="html5-qrcode-demo.js"></script>
</head>
@mebjas
mebjas / YuvConvertor.java
Created July 12, 2021 15:13
YUV_420_888 Image to Bitmap using ScriptIntrinsicYuvToRGB
class YuvConvertor {
private final Allocation in, out;
private final ScriptIntrinsicYuvToRGB script;
static {
System.loadLibrary("yuv2rgb-lib");
}
public YuvConvertor(Context context, int width, int height) {
RenderScript rs = RenderScript.create(context);
@mebjas
mebjas / yuv_to_rgb.3.java
Created July 6, 2021 11:14
YUV to ARGB Bitmap using Renderscript
static Bitmap yuv420ToBitmap(Image image, Context context) {
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicYuvToRGB script = ScriptIntrinsicYuvToRGB.create(
rs, Element.U8_4(rs));
// Refer the logic in a section below on how to convert a YUV_420_888 image
// to single channel flat 1D array. For sake of this example I'll abstract it
// as a method.
byte[] yuvByteArray = yuv420ToByteArray(image);
@mebjas
mebjas / post_36_skeleton.cc
Created January 1, 2022 14:03
Skeleton code of YUV to RGB
struct RGBA_8888 {
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a = 255;
};
RGBA_8888 yuv2rgb(uint8_t y, uint8_t u, uint8_t v) {
int r = y + (1.370705 * (v - 128));
int g = y - (0.698001 * (v - 128)) - (0.337633 * (u - 128));;
@mebjas
mebjas / image-loader-jni.cc
Created November 16, 2022 10:03
Code snippet for reading image using fd and decoding with our image decoder.
// Read the image
std::unique_ptr<Image> image = nullptr;
{
std::string image_buffer;
image_buffer.resize(fd_length);
int remaining_length = read(fd, &image_buffer[0], length);
if (remaining_length != 0) {
return env->NewStringUTF("Failed to read full image");
}
@mebjas
mebjas / image.h
Last active November 16, 2022 10:02
Updated interface of image library with method to decode Image from string image buffer.
class ImageFactory {
public:
// Decodes the `image_buffer` and returns Image instance.
//
// Suggestion for readers: Use absl::string_view instead of string here.
static std::unique_ptr<Image> FromString(const std::string& image_buffer);
// Creates an instance of 'Image' from the file descriptor 'fd'.
//
@mebjas
mebjas / image.cc
Created November 16, 2022 09:59
Implementation of ImageFactory in image library
#include "image.h"
#include <android/imagedecoder.h>
static std::unique_ptr<Image> ImageFactory::FromFd(int fd) {
// First create decoder from fd.
AImageDecoder* decoder;
int result = AImageDecoder_createFromFd(fd, &decoder);
if (result != ANDROID_IMAGE_DECODER_SUCCESS) {
// More info: https://developer.android.com/ndk/reference/group/image-decoder#aimagedecoder_createfromfd
@mebjas
mebjas / image.h
Created November 16, 2022 09:59
Header file for our image library
#include <memory>
#include <assert.h>
#include <android/imagedecoder.h>
// Data class for ARGB image (owns the memory associated with the image).
//
// Note for readers: Current implementation only allows read operations but can
// be extended to support write operations by overloading `()=` operator.
class Image {
@mebjas
mebjas / image-loader-jni.cc
Created November 16, 2022 09:57
Skeleton of JNI file for reading image in native layer.
#include <jni.h>
// Corresponding to NativeImageLoader class in
// dev.minhazav.samples package.
extern "C" JNIEXPORT jstring JNICALL
Java_dev_minhazav_samples_NativeImageLoader_readFile(
JNIEnv* env, jclass, jint fd) {
if (fd < 0) {
return env->NewStringUTF("Invalid fd");
}
@mebjas
mebjas / NativeImageLoader.java
Created November 16, 2022 09:55
Java part of JNI library for reading image in JNI
/** Wrapper class for loading image in native layer. */
public final class NativeImageLoader {
static {
System.loadLibrary("image-loader-jni");
}
/** Reads the image represented by {@code fd} in native layer.
*
* <p>For apparently no reason!