创建浮动动作按钮拍照

时间:2018-06-17 20:12:55

标签: android

我想创建一个名为(btn_fab)的浮动动作按钮从相机拍照我可以制作浮动动作按钮从图库中调用图像(mFabPickImage)并将此图像发送到segmentBitmap()函数以对其进行分割 问题是:我的(btn_fab)拍摄照片,但不会将其显示为碎片或创建分割 这是我初始化2个浮动操作按钮的代码

MainActivity.java

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

        setContentView(R.layout.activity_main);

        buckyButton = findViewById(R.id.buckysButton);


       //  src_img =(ImageView) findViewById(R.id.src_img) ;

       Intent intent=getIntent();

        String message=intent.getStringExtra("message");


        if(message=="hello"){

            initModel();


        }

      else if(message=="hi"){

            initModel2();

        }


        setCapture();
        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);

}

setupViews()从图库中获取图像,使用setCapture()从相机中获取图像

 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);


            }

        });
    }


 private void setCapture(){
    btn_fab=(FloatingActionButton)findViewById(R.id.fab);
    btn_fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            String root =Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString()+"propic.jpg";
            uri=Uri.parse(root);
            startActivityForResult(i,REQUEST_IMAGE_CAPTURE);

        }
    });


}

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();
            initModel2();

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


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

这是我的onActivityResult

 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);
        // Bundle extras= getIntent().getExtras();
        // Bitmap photo = (Bitmap) extras.get("data");
        // src_img.setImageBitmap(photo);
        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);//important note

    }

}

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

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

    }

}

 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)));
    }
}
private void setPickImageEnabled2(boolean enabled) {
    if (btn_fab != null) {
        btn_fab.setEnabled(enabled);

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

这是SegmentBitmapFragment.java

 public Loader<List<SegmentBitmap>> onCreateLoader(int id, Bundle args) {
    return new SegmentBitmapsLoader(getContext(), mImageUri);

}


public void segmentBitmap(Uri pickedImageUri) {

    mImageUri = pickedImageUri;

    fastDisplayOrigin();

    restartLoader();
}
public void segmentBitmap2(Uri imageCaptureUri) {
    mImageUri = imageCaptureUri;

    fastDisplayOrigin();

    restartLoader();
}


private void fastDisplayOrigin() {
    if (mRecyclerView != null) {
        mRecyclerView.scrollToPosition(0);
        RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
        if (adapter instanceof AbsArrayRecyclerAdapter) {
            AbsArrayRecyclerAdapter bitmapsAdapter =
                    (AbsArrayRecyclerAdapter)adapter;

            bitmapsAdapter.clear();
            bitmapsAdapter.add(new SegmentBitmap(R.string.label_original, mImageUri));
            bitmapsAdapter.add(new SegmentBitmap(R.string.label_mask, (Bitmap)null));
            bitmapsAdapter.add(new SegmentBitmap(R.string.label_cropped, (Bitmap)null));


        }
    }
}

0 个答案:

没有答案