Friday, 4 August 2017

Capture Image & Resize and adding Text WaterMark Android

    static final int REQUEST_IMAGE_CAPTURE = 1;
    Uri photoURI;
    File file;
    String imagepath;
    BitmapFactory.Options bmOptions;
    Bitmap bitmap;
private void dispatchTakePictureIntent() { if (loadingDialog != null && loadingDialog.isShowing()==false) loadingDialog.show(); Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); String root = Environment.getExternalStorageDirectory().toString(); File myDir = new File(root + "/saved_images"); myDir.mkdirs(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss"); String fileName = dateFormat.format(new Date()) + ".png"; file = new File(myDir, fileName); photoURI = Uri.fromFile(file); if (takePictureIntent.resolveActivity(getActivity().getPackageManager())
         != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

    public void resize(File file, String benchMark) {
        try {
            bmOptions = new BitmapFactory.Options();
            bmOptions.inJustDecodeBounds = false;
            bmOptions.inPreferredConfig = Bitmap.Config.RGB_565;
            bmOptions.inDither = true;
            bitmap = BitmapFactory.decodeFile(imagepath, bmOptions);
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();
            Log.e("width & Height", "width " + bitmap.getWidth());
            if (bitmap.getWidth() > 1200) {
                w = bitmap.getWidth() * 20 / 100;
                h = bitmap.getHeight() * 20 / 100;
            }

            Log.e("width & Height", "width " + w + " height " + h);
            bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);

            Canvas c = new Canvas(bitmap);
            Paint p = new Paint();
            p.setColor(getResources().getColor(R.color.bgcolor));
            p.setStyle(Paint.Style.FILL_AND_STROKE);
           // paint.setColor(Color.BLACK);            p.setTextSize(20);
            c.drawText(benchMark, 10, 20, p);

            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, bytes);
            try {
                Log.e("Compressing", "Compressing");
                FileOutputStream fo = new FileOutputStream(file);
                fo.write(bytes.toByteArray());
                fo.close();
            } catch (Exception e) {
                Log.e("Exception", "Image Resizing" + e.getMessage());
            }
    }

    catch(
    Exception e
    )

}


@Overridepublic void onActivityResult(int requestCode, int resultCode,
 Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
 if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == getActivity()
   .RESULT_OK)
 {
imagepath = photoURI.getPath();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
resize(file, dateFormat.format(date));

}
}

No comments:

Post a Comment