Skip to content

Instantly share code, notes, and snippets.

Avatar
🖥️
Working on a camera for everyone 🐝 #CameraGo

minhaz mebjas

🖥️
Working on a camera for everyone 🐝 #CameraGo
View GitHub Profile
@mebjas
mebjas / image-loader-jni.cc
Created Nov 16, 2022
Code snippet for reading image using fd and decoding with our image decoder.
View image-loader-jni.cc
// 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 Nov 16, 2022
Updated interface of image library with method to decode Image from string image buffer.
View image.h
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 Nov 16, 2022
Implementation of ImageFactory in image library
View image.cc
#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 Nov 16, 2022
Header file for our image library
View image.h
#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 Nov 16, 2022
Skeleton of JNI file for reading image in native layer.
View image-loader-jni.cc
#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 Nov 16, 2022
Java part of JNI library for reading image in JNI
View NativeImageLoader.java
/** 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!
@mebjas
mebjas / GettingFd.java
Created Nov 16, 2022
Code snippet for getting fd value from Uri in Android.
View GettingFd.java
Context context = getApplicationContext();
ContentResolver contentResolver = context.getContentResolver();
try (AssetFileDescriptor assetFileDescriptor
= contentResolver.openAssetFileDescriptor(imageUri, "r")) {
ParcelFileDescriptor parcelFileDescriptor = assetFileDescriptor.getParcelFileDescriptor();
int fd = parcelFileDescriptor.getFd();
// TODO: Read file using the fd in native layer.
// Important: Native layer shouldn't assume ownership of this fd and close it.
@mebjas
mebjas / MainActivity.java
Created Nov 16, 2022
Example of Android activity with image picker.
View MainActivity.java
public class MainActivity extends AppCompatActivity {
private final ActivityResultLauncher<String[]> galleryActivityLauncher
= registerForActivityResult(new ActivityResultContracts.OpenDocument(),
this::onPickImage);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@mebjas
mebjas / html5_qrcode.enableTorch.ts
Created Oct 30, 2022
Snippet to enable torch on running video track in Html5Qrcode library
View html5_qrcode.enableTorch.ts
let constraints: MediaTrackConstraints = {
"torch": true,
"advanced": [{ "torch": true }]
};
await html5Qrcode.applyVideoConstraints(constraints);
let settings = html5Qrcode.getRunningTrackSettings();
if (settings.torch === true) {
// Torch was indeed enabled, succeess.
@mebjas
mebjas / html5_qrcode.isTorchSupported.ts
Created Oct 30, 2022
Helper function to check if torch is supported on the given video track.
View html5_qrcode.isTorchSupported.ts
/** Returns {@code true} if torch is supported. */
function isTorchSupported(html5Qrcode: Html5Qrcode): boolean {
let settings = html5Qrcode.getRunningTrackSettings();
return "torch" in settings;
}