CameraX逐像素检测像素颜色

问题描述

我使用了基本的CameraX示例,能够使用ImageAnalysis例程并逐像素遍历完整的位图,以检测具有给定基本颜色的颜色相似性。 我想要的是以灰度显示预览(所选颜色除外)。 我使用“位图bMap = txView.getBitmap();”获得位图。在查看完位图并更改了每个像素后,我使用“ img.setimageBitmap(bMap);”将位图发送到名为“ img”的Imagview。 我在顶部有我的TextureView INVISIBLE和Imageview以显示修改后的位图。 如果我只得到位图并显示它,我没有问题。但是,当我添加嵌套循环来检查每个像素时,它只需要很长时间。

这是我的代码

    private void startCamera() {
        //make sure there isn't another camera instance running before starting
        CameraX.unbindAll();

        /* start preview */
        int aspRatioW = 480; //txView.getWidth(); //get width of screen
        int aspRatioH = 640;//txView.getHeight(); //get height
        Rational asp = new Rational (aspRatioW,aspRatioH); //aspect ratio
        Size screen = new Size(aspRatioW,aspRatioH); //size of the screen

        //config obj for preview/viewfinder.
        PreviewConfig pConfig = new PreviewConfig.Builder().setTargetAspectRatio(asp).setTargetResolution(screen).build();
        Preview preview = new Preview(pConfig); //lets build it

        preview.setonPreviewOutputUpdateListener(
                new Preview.OnPreviewOutputUpdateListener() {
                    //to update the surface texture we have to destroy it first,then re-add it
                    @Override
                    public void onUpdated(Preview.PreviewOutput output){
                        ViewGroup parent = (ViewGroup) txView.getParent();
                        parent.removeView(txView);
                        parent.addView(txView,0);

                        txView.setSurfaceTexture(output.getSurfaceTexture());
                        updateTransform();
                    }
                });

        /* image capture */

        //config obj,selected capture mode
        ImageCaptureConfig imgCapConfig = new ImageCaptureConfig.Builder().setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
                .setTargetRotation(getwindowManager().getDefaultdisplay().getRotation()).build();
        final ImageCapture imgCap = new ImageCapture(imgCapConfig);

        findViewById(R.id.capture_button).setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = new File(Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + ".jpg");
                imgCap.takePicture(file,new ImageCapture.OnImageSavedListener() {
                    @Override
                    public void onImageSaved(@NonNull File file) {
                        String msg = "Photo capture succeeded: " + file.getAbsolutePath();
                        Toast.makeText(getBaseContext(),msg,Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(@NonNull ImageCapture.UseCaseError useCaseError,@NonNull String message,@Nullable Throwable cause) {
                        String msg = "Photo capture Failed: " + message;
                        Toast.makeText(getBaseContext(),Toast.LENGTH_LONG).show();
                        if(cause != null){
                            cause.printstacktrace();
                        }
                    }
                });
            }
        });

        /* image analyser */

        ImageAnalysisConfig imgAConfig = new ImageAnalysisConfig.Builder().setimageReaderMode(ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE).build();
        ImageAnalysis analysis = new ImageAnalysis(imgAConfig);

        analysis.setAnalyzer(
            new ImageAnalysis.Analyzer(){
                @Override
                public void analyze(ImageProxy image,int rotationdegrees){

                    //https://www.codota.com/code/java/methods/android.media.Image/getPlanes
                    //ByteBuffer yBuffer = image.getPlanes()[0].getBuffer(); //  planes[0].buffer // Y
                    //ByteBuffer uBuffer = image.getPlanes()[1].getBuffer(); //  planes[1].buffer // U
                    //ByteBuffer vBuffer = image.getPlanes()[2].getBuffer(); //  planes[2].buffer // V
                    //byte[] yBufferBytes= new byte[yBuffer.remaining()]; // .remaining is for the size of the buffer

                    Bitmap bMap = txView.getBitmap();
                    if (bMap != null){
                        int Hsize = bMap.getHeight();
                        int Wsize = bMap.getWidth();
                        int bmPixel,alpha,redValue,blueValue,greenValue;
                        int bt_alpha,bt_redValue,bt_blueValue,bt_greenValue;
                        bt_alpha = Color.alpha(mDefaultColor);
                        bt_redValue = Color.red(mDefaultColor);
                        bt_blueValue = Color.blue(mDefaultColor);
                        bt_greenValue = Color.green(mDefaultColor);

                        for (int h = 0; h < Hsize; h++){
                            for (int w = 0; w < Wsize; w++){
                                bmPixel = bMap.getPixel(w,h);
                                alpha = Color.alpha(bmPixel);
                                redValue = Color.red(bmPixel);
                                blueValue = Color.blue(bmPixel);
                                greenValue = Color.green(bmPixel);
                                double d2 = Math.sqrt((0.3*(bt_redValue-redValue)*(bt_redValue-redValue)) + (0.59*(bt_greenValue-greenValue)*(bt_greenValue-greenValue)) + (0.11*(bt_blueValue-blueValue)*(bt_blueValue-blueValue)));
                               if (d2 > 10){
                                    int newRed,newBlue,newGreen,bmGrey;
                                    bmGrey = (redValue + blueValue + greenValue)/3;
                                    newRed = bmGrey;
                                    newBlue = bmGrey;
                                    newGreen = bmGrey;
                                    bMap.setPixel(w,h,Color.argb(alpha,newRed,newBlue));
                                }
                            }
                        }
                        ImageView img = (ImageView) findViewById(R.id.image_Changed);
                        img.setimageBitmap(bMap);
                    }

                    image.close();
                    // https://stackoverflow.com/questions/36212904/yuv-420-888-interpretation-on-samsung-galaxy-s7-camera2

                    //y'all can add code to analyse stuff here idek go wild.
                }
            });

        //bind to lifecycle:
        CameraX.bindToLifecycle((LifecycleOwner)this,analysis,imgCap,preview);
    }

关于如何使其更快运行的任何想法?

PS:很抱歉有这么多评论行。我一直在进行很多测试,并在许多地方寻求帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)