This file contains hidden or 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
// 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"); | |
} |
This file contains hidden or 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
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'. | |
// |
This file contains hidden or 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
#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 |
This file contains hidden or 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
#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 { |
This file contains hidden or 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
#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"); | |
} |
This file contains hidden or 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
/** 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! |
This file contains hidden or 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
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. |
This file contains hidden or 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
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); |
This file contains hidden or 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
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. |
This file contains hidden or 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
/** Returns {@code true} if torch is supported. */ | |
function isTorchSupported(html5Qrcode: Html5Qrcode): boolean { | |
let settings = html5Qrcode.getRunningTrackSettings(); | |
return "torch" in settings; | |
} |
NewerOlder