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 / GettingFd.java
Created November 16, 2022 09:54
Code snippet for getting fd value from Uri in Android.
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 November 16, 2022 09:52
Example of Android activity with image picker.
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 October 30, 2022 14:19
Snippet to enable torch on running video track in Html5Qrcode library
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 October 30, 2022 14:18
Helper function to check if torch is supported on the given video track.
/** Returns {@code true} if torch is supported. */
function isTorchSupported(html5Qrcode: Html5Qrcode): boolean {
let settings = html5Qrcode.getRunningTrackSettings();
return "torch" in settings;
}
@mebjas
mebjas / html5_qrcode.torch.js
Created October 30, 2022 14:17
Config for enabling torch with Html5QrcodeScanner
let html5QrcodeScanner = new Html5QrcodeScanner(
"reader",
{
fps: 10,
qrbox: {width: 250, height: 250},
rememberLastUsedCamera: true,
aspectRatio: 1.7777778,
showTorchButtonIfSupported: true
});
@mebjas
mebjas / float_check_e.cc
Created October 5, 2022 10:34
Printing output of difference of exact same values
float a = 100.151f + 0.151f;
float b = 100.101f + 0.201f;
std::cout << "a = " << a << "; b = " << b << "\n";
float diff = a - b;
std::cout << "Diff = " << diff << "\n";
@mebjas
mebjas / float_check_d.cc
Created October 5, 2022 09:42
modified version of isNearlyEqual(..) that works relative to the value of input floating values.
inline bool isNearlyEqual(float val_a, float val_b, float max_relative_diff = FLT_EPSILON) {
float abs_diff = abs(val_a - val_b);
val_a = fabs(val_a);
val_b = fabs(val_b);
float scaled_epsilon = max_relative_diff * max(val_a, val_b);
return abs_diff <= scaled_epsilon;
}
@mebjas
mebjas / float_check_c.cc
Created October 5, 2022 09:34
isNearlyEqual doesn't work well for larger floating values when using FLT_EPSILON
float a = 100.151f + 0.151f;
float b = 100.101f + 0.201f;
bool d = isNearlyEqual(a, b);
@mebjas
mebjas / float_check_b.cc
Created October 5, 2022 09:26
Code to test the problem above about 10 million times.
#include <iostream>
#include <math.h>
#include <float.h>
using namespace std;
inline bool isNearlyEqual(float val_a, float val_b) {
return fabs(val_a - val_b) < FLT_EPSILON;
}
@mebjas
mebjas / float_check_a.cc
Created October 5, 2022 02:42
Code with equality check on floating values
constexpr int kIterations = 10000000;
int counter = 0;
for (int i = 0; i < kIterations; ++i) {
float a = 0.151f + 0.151f;
float b = 0.101f + 0.201f;
bool d = (a == b);
if (d) {