jenly1314 / ZXingLite

🔥 ZXing的精简极速版,优化扫码和生成二维码/条形码,内置闪光灯等功能。扫描风格支持:微信的线条样式,支付宝的网格样式。几句代码轻松拥有扫码功能 ,ZXingLite让集成更简单。(扫码识别速度快如微信)
https://jenly1314.github.io/ZXingLite/
Apache License 2.0
3.03k stars 470 forks source link

How to detect black screen #214

Closed Agmcz closed 1 year ago

Agmcz commented 1 year ago

I want to get a screenshot (bitmap) to check the pixels from CameraScan, How can fetch bitmap!!!

jenly1314 commented 1 year ago

I want to get a screenshot (bitmap) to check the pixels from CameraScan, How can fetch bitmap!!!

previewView.getBitmap() Get a bitmap from the preview view. Is this what you need?

Agmcz commented 1 year ago

I want to something like this example

            running = true;
            thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (running) {
                        try {
                            Bitmap bitmap = binding.previewView.getBitmap();

                            int[] histogram = new int[256];
                            // int histogram[256];
                            for (int i=0;i<256;i++) {
                                histogram[i] = 0;
                            }

                            for (int x = 0; x < bitmap.getWidth(); x++) {
                                for(int y = 0; y < bitmap.getHeight(); y++) {
                                    int color = bitmap.getPixel(x, y);

                                    int r = Color.red(color);
                                    int g = Color.green(color);
                                    int b = Color.blue(color);

                                    int brightness = (int) (0.2126*r + 0.7152*g + 0.0722*b);
                                    histogram[brightness]++;
                                }
                            }

                            int allPixelsCount = bitmap.getWidth() * bitmap.getHeight();

                            // Count pixels with brightness less then 10
                            int darkPixelCount = 0;
                            for (int i=0;i<10;i++) {
                                darkPixelCount += histogram[i];
                            }

                            if (darkPixelCount > allPixelsCount * 0.25) {
                                // Dark picture. Play with a percentag
                                // Turn on flash
                            }
                            else {
                                // Light picture.
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            thread.start();
Agmcz commented 1 year ago

I'm trying to get X,Y of frame (ViewfinderView) to create a small bitmap for fast search...

for example:

Bitmap bitmap = Bitmap.createBitmap(frame.X, frame.X, ...); if (isDark(bitmap )) { // trun on flash } else { // trun off flash }

can help me and thanks in adavnce.

Agmcz commented 1 year ago

I used "previewView.getBitmap();"

modified "ViewfinderView.java" (private Rect frame;) to (public Rect frame;) for using from inside MainActivity...

Bitmap bitmap = Bitmap.createBitmap(binding.previewView.getBitmap(), binding.viewfinderView.frame.left, binding.viewfinderView.frame.top ,binding.viewfinderView.frame.width(), binding.viewfinderView.frame.height());

but "previewView.getBitmap();" return null!!!

I want to get a bitmap from camerascan and crop bitmap from frame dimensions.

jenly1314 commented 1 year ago

If obtaining brightness is to switch the flash, it is recommended that you use a sensor to detect changes in brightness.

See: AmbientLightManager

Agmcz commented 1 year ago

Thanks.