如何在onActivityResult上设置bitmap1变量的模糊效果?

时间:2016-07-02 08:56:56

标签: android bitmap blur onactivityresult

    public class Fram_1 extends AppCompatActivity {
        private int REQUEST_CAMERA = 0, SELECT_FILE = 1, SELECT_FILE2 = 2;
        private String userChoosenTask;
        ImageView img1, img2;
        TextView txt1, txt2;
        LinearLayout img_store, txt_effect, img_brighteff;
        FrameLayout layout_container;
        View view;
        Bitmap bitmap1, bitmap2;
        Toolbar toolbarMain;
        EditText edtxt_txteffect;
        TextView txt_txteffect;


        LinearLayout effect_toolbarlayoutContainer;

        //      for Birghtness Effect
        private SeekBar seekBarBri, seekBarBlur;

        //Variable to store brightness value
        private int brightness;

        //Content resolver used as a handle to the system's settings
        private ContentResolver cResolver;
        //Window object, that will store a reference to the current window
        private Window window;

        //    for Blur effect

        private Bitmap bit_blur;
     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_fram_1);

    img_brighteff = (LinearLayout) findViewById(R.id.frm1_img_effect);
    txt_effect = (LinearLayout) findViewById(R.id.frm1_text_effect);
            img_store = (LinearLayout) findViewById(R.id.frm1_toolbar_image_save);
     img1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    txt1.setVisibility(View.GONE);
                    selectImage(SELECT_FILE);

                }
            });
    //                For Blur Effect
                    seekBarBlur.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                        @Override
                        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                        }

                        @Override
                        public void onStartTrackingTouch(SeekBar seekBar) {

                        }

                        @Override
                        public void onStopTrackingTouch(SeekBar seekBar) {
                            float radius = (float) Fram_1.this.seekBarBlur.getProgress();
                            img1.setImageBitmap(createBlurBitmap(bitmap1, radius));
                        }
                    });


                }
            });


            client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
        }
    }
     private void selectImage(final int number) {
            final CharSequence[] items = {"Take Photo", "Choose from Library",
                    "Cancel"};

            AlertDialog.Builder builder = new AlertDialog.Builder(Fram_1.this);
            builder.setTitle("Add Photo!");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {
                    boolean result = Utility.checkPermission(Fram_1.this);

                    if (items[item].equals("Take Photo")) {
                        userChoosenTask = "Take Photo";
                        if (result)
                            cameraIntent();

                    } else if (items[item].equals("Choose from Library")) {
                        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        intent.setType("image/*");
                        startActivityForResult(Intent.createChooser(intent, "Select File"), number);


                    } else if (items[item].equals("Cancel")) {
                        dialog.dismiss();
                    }
                }
            });
            builder.show();
        }
     @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            switch (requestCode) {
                case Utility.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        if (userChoosenTask.equals("Take Photo"))
                            cameraIntent();
                        else if (userChoosenTask.equals("Choose from Library")) {
                            Intent intent1 = new Intent();
                            intent1.setType("image/*");
                            intent1.setAction(Intent.ACTION_GET_CONTENT);//
                            startActivityForResult(Intent.createChooser(intent1, "Select File"), SELECT_FILE);
                        }

                    } else {
                        //code for deny
                    }
                    break;
            }
        }
    }
 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImageUri = data.getData();
            if (requestCode == SELECT_FILE) {
                try {
// iwant to use bitmap1 variable inside seekBarBlur listener onstopTakingTouch setImageonBitmap methhod
                    bitmap1 = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);

                    img1.setScaleType(ImageView.ScaleType.FIT_XY);
                    img1.setImageBitmap(bitmap1);
                    txt1.setVisibility(View.GONE);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else if (requestCode == SELECT_FILE2) {
                try {
                    bitmap2 = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                img2.setScaleType(ImageView.ScaleType.FIT_XY);
                img2.setImageBitmap(bitmap2);
                txt2.setVisibility(View.GONE);
            }


        } else if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {

            onCaptureImageResult(data);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

您可以这样做:

private static final float BITMAP_SCALE = 0.4f;
private static final float BLUR_RADIUS = 7.5f;

public static Bitmap blur(Context context, Bitmap image) {
    int width = Math.round(image.getWidth() * BITMAP_SCALE);
    int height = Math.round(image.getHeight() * BITMAP_SCALE);

    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

    RenderScript rs = RenderScript.create(context);
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
    theIntrinsic.setRadius(BLUR_RADIUS);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}

并在onActivityResult方法中使用此方法来模糊Bitmap

Here's有关如何在Android中模糊图像的教程。