在onSurfaceTextureUpdated()期间更改背景的颜色

时间:2018-07-28 00:23:48

标签: android android-camera firebase-mlkit

我正在使用ML kit的面部检测API。我希望在onSurfaceTextureUpdated()方法中更改背景的颜色。

public void onSurfaceTextureUpdated(SurfaceTexture texture) {
        if (lookForFace){
            image = FirebaseVisionImage.fromBitmap(mTextureView.getBitmap());
            if (detector != null) {
                detector.detectInImage(image)
                        .addOnSuccessListener(firebaseVisionFaces -> {
                           if(firebaseVisionFaces.size() > 0){
                                FirebaseVisionFace face = firebaseVisionFaces.get(0);
                                if (face.getSmilingProbability() != FirebaseVisionFace.UNCOMPUTED_PROBABILITY) {
                                    float smiling = face.getSmilingProbability();
                                    Log.i("hjkhjkhkjhkjhjk", "smiling " + smiling);
                                    int r = (int) smiling * 255;
                                    int g = (int) smiling * 215;
                                    int b = (int) smiling * 255;
                                    int color = Color.rgb(r,g,b);
                                    Log.i("hjkhjkhkjhkjhjk", "color " + color);
                                    int color2 = fragment.getResources().getColor(R.color.pink);
                                    int color3 = Color.rgb(255,215,255);
                                    if (smiling >0.90f)
                                    ((ProfilePictureFragment) fragment).bind.background.setBackgroundColor(color3);
                                }
                            }
                        })
                        .addOnFailureListener(e -> Log.i("hjkhjkhkjhkjhjk", e.toString()));
            }
        }
    }

每次调用此方法时,都会为smiling生成一个新值。如果存在if语句,则背景颜色会发生变化,尽管变化缓慢。当我尝试在每个方法调用期间更改背景色时,不会发生任何更改。我的直觉告诉我使用HandlerThread进行计算,然后更新UI。但是,我也觉得好像有些事情我没有考虑。有什么建议吗?

0 个答案:

没有答案