从相机捕获图像并在对其进行分割后将其显示为片段

时间:2018-06-13 00:24:06

标签: android

我有一个名为segmentBitmap2()的函数,它接受图像Uri并对其进行分割。我可以对画廊中的图片进行分割,但现在我想以相同的方式从相机拍照,但是虽然我初始化了相机并获得了拍摄的图像Uri,但它没有工作。我该怎么办?

这是我的图片捕获代码和从图库中挑选的图片:

MainActivity.java

 public class InitializeModelAsyncTask extends AsyncTask<Void, Void, Boolean> {


        @Override
        protected Boolean doInBackground(Void... voids) {
            final boolean ret = DeeplabModel.initialize();
            final boolean ret2 = DeeplabModel2.initialize();
            Logger.debug("initialize deeplab model: %s", ret);
            Logger.debug("initialize deeplab model: %s", ret2);
            if (ret) {
                return ret;
            } else {
                return ret2;
            }
        }
    }


private FloatingActionButton mFabPickImage;

static final int REQUEST_IMAGE_CAPTURE=1;

    public void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


         Button buckyButton =(Button) findViewById(R.id.buckysButton) ;


        setupViews();
    }




private boolean hasCamera(){
    return getPackageManager() .hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
public void launchCamera(View view){
   Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
   startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);

}

private void setupViews() {

    mFabPickImage = findViewById(R.id.fab_pick_image);
    if (mFabPickImage != null) {
        mFabPickImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent;

                if (Build.VERSION.SDK_INT >= 19) {
                    intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
                    intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
                } else {
                    intent = new Intent(Intent.ACTION_GET_CONTENT);
                }

                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setType("image/*");

                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

                ActivityLauncher.launchActivityForResult(MainActivity.this,
                        Intent.createChooser(intent, getString(R.string.app_name)),
                        REQUEST_PICK_IMAGE);
            }

        });
    }



}

@Override
protected void onStart() {
    super.onStart();

    syncUIWithPermissions(true);
}

@Override
protected void onResume() {
    super.onResume();

    syncUIWithPermissions(false);
}



 private void syncUIWithPermissions(boolean requestIfNeed) {
    final boolean granted = checkRequiredPermissions(requestIfNeed);
       setPickImageEnabled(granted);
        if (granted && !DeeplabModel.isInitialized()) {
            initModel();
        }
      else if (granted && !DeeplabModel2.isInitialized()) {
            initModel();
        }

    }


private boolean checkRequiredPermissions() {
    return checkRequiredPermissions(false);
}

private boolean checkRequiredPermissions(boolean requestIfNeed) {
    final boolean writeStoragePermGranted =
            ContextCompat.checkSelfPermission(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        == PackageManager.PERMISSION_GRANTED;

    Logger.debug("storage permission granted: %s", writeStoragePermGranted);

    if (!writeStoragePermGranted
            && requestIfNeed) {
        requestRequiredPermissions();
    }

    return writeStoragePermGranted;
}

private void requestRequiredPermissions() {
    ActivityCompat.requestPermissions(this,
            new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
            },
            REQUEST_REQUIRED_PERMISSION);
}


 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    Logger.debug("requestCode = 0x%02x, permission = [%s], grant = [%s]",
            requestCode,
            ArrayUtils.stringArrayToString(permissions, ","),
            ArrayUtils.intArrayToString(grantResults));
    if (requestCode == REQUEST_REQUIRED_PERMISSION) {
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Logger.debug("permission granted, initialize model.");
            initModel();

            if (mFabPickImage != null) {
                mFabPickImage.setEnabled(true);
                mFabPickImage.setBackgroundTintList(
                        ColorStateList.valueOf(getColor(R.color.colorAccent)));
            }
        } else {
            Logger.debug("permission denied, disable fab.");
        }


        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        Logger.debug("requestCode = %d, resultCode = %d, data = %s",
                requestCode,
                resultCode,
                data);
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

        Uri ImageCaptureUri=data.getData();
        Logger.debug("capture: %s", ImageCaptureUri);
                    if (ImageCaptureUri != null) {
           if (Build.VERSION.SDK_INT >= 19) {
                final int takeFlags = data.getFlags() & Intent.FLAG_GRANT_READ_URI_PERMISSION;
                getContentResolver()
                        .takePersistableUriPermission(ImageCaptureUri, takeFlags);
            }

            segmentImage2(ImageCaptureUri);
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }

    Logger.debug("requestCode = %d, resultCode = %d, data = %s",
            requestCode,
            resultCode,
            data);

        if (requestCode == REQUEST_PICK_IMAGE && resultCode == RESULT_OK) {

            Uri pickedImageUri = data.getData();
            Logger.debug("picked: %s", pickedImageUri);

            if (pickedImageUri != null) {
                if (Build.VERSION.SDK_INT >= 19) {
                    final int takeFlags = data.getFlags() & Intent.FLAG_GRANT_READ_URI_PERMISSION;
                    getContentResolver()
                            .takePersistableUriPermission(pickedImageUri, takeFlags);
                }

                segmentImage(pickedImageUri);
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }


private void segmentImage(Uri pickedImageUri ) {
    Fragment fragment = findFragment(R.id.fragment_segment_bitmaps);

    if (fragment instanceof SegmentBitmapsFragment) {
        ((SegmentBitmapsFragment)fragment).segmentBitmap(pickedImageUri);
    }

}
private void segmentImage2(Uri ImageCaptureUri ) {
    Fragment fragment = findFragment(R.id.fragment_segment_bitmaps);

    if (fragment instanceof SegmentBitmapsFragment) {
        ((SegmentBitmapsFragment)fragment).segmentBitmap(ImageCaptureUri);//important note

    }

}

private void initModel() {
    new InitializeModelAsyncTask().execute((Void)null);
}


private void setPickImageEnabled(boolean enabled) {
    if (mFabPickImage != null) {
        mFabPickImage.setEnabled(enabled);

        int resId = enabled ? R.color.colorAccent : R.color.light_gray;
        mFabPickImage.setBackgroundTintList(
                ColorStateList.valueOf(getColor(resId)));
    }
}

}

0 个答案:

没有答案