HMS-Core / hms-ml-demo

HMS ML Demo provides an example of integrating Huawei ML Kit service into applications. This example demonstrates how to integrate services provided by ML Kit, such as face detection, text recognition, image segmentation, asr, and tts.
https://developer.huawei.com/consumer/en/hms/huawei-mlkit?ha_source=hms1
Apache License 2.0
356 stars 120 forks source link

The GraphicOverlay width changes when trying to save the LensEnginePreview as Bitmap #76

Closed Mouadabdelghafouraitali closed 3 years ago

Mouadabdelghafouraitali commented 3 years ago

Hi, I'm using the Gesture-Change-Background as a main source on my project, but I ran into a problem, when I try to save the LensEnginePreview as Bitmap, the GraphicOverlayimage width changes :

Why this happening?

SoftwareGift commented 3 years ago

Could you show me your code? The possible cause is that the value range should be 0-255.We've done some post-processing on the images.Please carefully find the relevant code in the project.

Mouadabdelghafouraitali commented 3 years ago

@SoftwareGift thank you for your comment, please let me explain the whole scenario, because there's no way to record the video (record what happen inside LensEnginePreview), I tried to capture the view and converting it to Bitmap, every second I pass the bitmap to MediaRecorderin order to generate a video file.

The issue, is related to the Bitmap inside GraphicOverlay, I don't know why but when I try to convert the LensEnginePreview to Bitmap The image loses its width.

Regarding the code : I've tried to use the same code in the Gesture-Change-Background and the same issue happened.

View to Bitmap (Using Kotlin KTX) :

    fun view2Bitmap(lens: LensEnginePreview): Bitmap {
        return lens.drawToBitmap()
    }

View to Bitmap :

public static Bitmap view2Bitmap(final View view) {
        if (view == null) return null;
        boolean drawingCacheEnabled = view.isDrawingCacheEnabled();
        boolean willNotCacheDrawing = view.willNotCacheDrawing();
        view.setDrawingCacheEnabled(true);
        view.setWillNotCacheDrawing(false);
        Bitmap drawingCache = view.getDrawingCache();
        Bitmap bitmap;
        if (null == drawingCache || drawingCache.isRecycled()) {
            view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
            view.buildDrawingCache();
            drawingCache = view.getDrawingCache();
            if (null == drawingCache || drawingCache.isRecycled()) {
                bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.RGB_565);
                Canvas canvas = new Canvas(bitmap);
                view.draw(canvas);
            } else {
                bitmap = Bitmap.createBitmap(drawingCache);
            }
        } else {
            bitmap = Bitmap.createBitmap(drawingCache);
        }
        view.setWillNotCacheDrawing(willNotCacheDrawing);
        view.setDrawingCacheEnabled(drawingCacheEnabled);
        return bitmap;
    }

please use one of the above code in the Gesture-Change-Background project, and you will see what I'm talking about.

Thank you

SoftwareGift commented 3 years ago

The pixel value of the image ranges from 0 to 255. You can write the bitmap value to a txt file and see if the value is the same as you expected.

Mouadabdelghafouraitali commented 3 years ago

I've managed to fix this issue, thank you