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.

Home Page:https://developer.huawei.com/consumer/en/hms/huawei-mlkit?ha_source=hms1

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

Mouadabdelghafouraitali opened this issue · comments

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 GraphicOverlay image width changes :

Why this happening?

commented

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.

@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 MediaRecorder in 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

commented

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.

I've managed to fix this issue, thank you