|
| 1 | + |
| 2 | + |
| 3 | + /** |
| 4 | + * Insert image into SD card |
| 5 | + **/ |
| 6 | + |
| 7 | + public static void insertImageInSDCard(Bitmap bp, String name) { |
| 8 | + |
| 9 | + /** Scale Down **/ |
| 10 | + float maxImageSize = 1200; |
| 11 | + |
| 12 | + float ratio = Math.min( |
| 13 | + (float) maxImageSize / bp.getWidth(), |
| 14 | + (float) maxImageSize / bp.getHeight()); |
| 15 | + int width = Math.round((float) ratio * bp.getWidth()); |
| 16 | + int height = Math.round((float) ratio * bp.getHeight()); |
| 17 | + |
| 18 | + Bitmap newBp = Bitmap.createScaledBitmap(bp, width, height, true); |
| 19 | + |
| 20 | + File f = new File(Environment.getExternalStorageDirectory(), Config.MEDIA_FOLDER); |
| 21 | + if (!f.exists()) { |
| 22 | + f.mkdirs(); |
| 23 | + } |
| 24 | + |
| 25 | + File sdCardDirectory = Environment.getExternalStorageDirectory(); |
| 26 | + File image = new File(sdCardDirectory.toString(), Config.MEDIA_FOLDER + name + ".jpg"); |
| 27 | + if (image.exists()) { |
| 28 | + image.delete(); |
| 29 | + } |
| 30 | + |
| 31 | + // Encode the file as a PNG image. |
| 32 | + FileOutputStream outStream; |
| 33 | + try { |
| 34 | + outStream = new FileOutputStream(image); |
| 35 | + newBp.compress(Bitmap.CompressFormat.JPEG, 60, outStream); |
| 36 | + /* 100 to keep full quality of the image */ |
| 37 | + |
| 38 | + outStream.flush(); |
| 39 | + outStream.close(); |
| 40 | + } catch (IOException e) { |
| 41 | + e.printStackTrace(); |
| 42 | + } |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + /** |
| 49 | + * Read image from external memory. |
| 50 | + **/ |
| 51 | + |
| 52 | + public static Bitmap readImageFromExternalMemory(String imageName) throws IOException { |
| 53 | + File file = new File(Environment.getExternalStorageDirectory().toString() + "/.nomedia/appName" + imageName + ".jpg"); |
| 54 | + FileInputStream inStream = new FileInputStream(file); |
| 55 | + |
| 56 | + Bitmap bitmap = BitmapFactory.decodeStream(inStream); |
| 57 | + inStream.close(); |
| 58 | + |
| 59 | + return bitmap; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Scale Down Bitmap |
| 64 | + **/ |
| 65 | + |
| 66 | + public static Bitmap scaledownBitmap(Bitmap bmp, int size) { |
| 67 | + |
| 68 | + int height = size; |
| 69 | + |
| 70 | + Bitmap background = Bitmap.createBitmap((int)height, (int)height, android.graphics.Bitmap.Config.ARGB_8888); |
| 71 | + |
| 72 | + float originalWidth = bmp.getWidth(); |
| 73 | + float originalHeight = bmp.getHeight(); |
| 74 | + |
| 75 | + Canvas canvas = new Canvas(background); |
| 76 | + |
| 77 | + float scale = height / originalWidth; |
| 78 | + |
| 79 | + float xTranslation = 0.0f; |
| 80 | + float yTranslation = (height - originalHeight * scale) / 2.0f; |
| 81 | + |
| 82 | + Matrix transformation = new Matrix(); |
| 83 | + transformation.postTranslate(xTranslation, yTranslation); |
| 84 | + transformation.preScale(scale, scale); |
| 85 | + |
| 86 | + Paint paint = new Paint(); |
| 87 | + paint.setFilterBitmap(true); |
| 88 | + |
| 89 | + canvas.drawBitmap(bmp, transformation, paint); |
| 90 | + |
| 91 | + return background; |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + public static Bitmap rotateImage(Bitmap source, float angle){ |
| 98 | + Matrix matrix = new Matrix(); |
| 99 | + matrix.postRotate(angle); |
| 100 | + return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), |
| 101 | + matrix, true); |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +/** Taking Image from Camera **/ |
| 110 | + |
| 111 | + Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 112 | + takePicture.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFile()); |
| 113 | + startActivityForResult(takePicture, image); |
| 114 | + |
| 115 | + /** With Permission **/ |
| 116 | + MarshMallowPermission marshMallowPermission = new MarshMallowPermission(getActivity()); |
| 117 | + if(!marshMallowPermission.checkPermissionForCamera()){ |
| 118 | + marshMallowPermission.requestPermissionForCamera(); |
| 119 | + }else { |
| 120 | + Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 121 | + takePicture.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFile()); |
| 122 | + startActivityForResult(takePicture, image); |
| 123 | + } |
| 124 | + |
| 125 | + /** Image Path Uri **/ |
| 126 | + |
| 127 | + private Uri getOutputMediaFile(){ |
| 128 | + String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); |
| 129 | + String imageFileName = timeStamp + ".jpg"; |
| 130 | + File storageDir = Environment.getExternalStoragePublicDirectory( |
| 131 | + Environment.DIRECTORY_PICTURES); |
| 132 | + pictureImagePath = storageDir.getAbsolutePath() + "/" + imageFileName; |
| 133 | + File file = new File(pictureImagePath); |
| 134 | + return Uri.fromFile(file); |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + /** Getting the Image **/ |
| 141 | + public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 142 | + super.onActivityResult(requestCode, resultCode, data); |
| 143 | + switch(requestCode) { |
| 144 | + case 1: |
| 145 | + |
| 146 | + if(resultCode == RESULT_OK){ |
| 147 | + try { |
| 148 | + Bitmap bitmap; |
| 149 | + File imgFile = new File(pictureImagePath); |
| 150 | + if(imgFile.exists()){ |
| 151 | + /** For Camera **/ |
| 152 | + bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); |
| 153 | + }else{ |
| 154 | + /** For from Gallery **/ |
| 155 | + Uri pickedImage = data.getData(); |
| 156 | + bitmap = decodeUri(pickedImage); |
| 157 | + } |
| 158 | + image2.setImageBitmap(bitmap); |
| 159 | + isImageView2Set = true; |
| 160 | + imageSetArray.add("img2"); |
| 161 | + }catch (Exception e){ |
| 162 | + e.printStackTrace(); |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | + default: |
| 167 | + break; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + /** Decode Uri **/ |
| 172 | + |
| 173 | + private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException { |
| 174 | + BitmapFactory.Options o = new BitmapFactory.Options(); |
| 175 | + o.inJustDecodeBounds = true; |
| 176 | + BitmapFactory.decodeStream( |
| 177 | + getActivity().getContentResolver().openInputStream(selectedImage), null, o); |
| 178 | + |
| 179 | + final int REQUIRED_SIZE = 100; |
| 180 | + |
| 181 | + int width_tmp = o.outWidth, height_tmp = o.outHeight; |
| 182 | + int scale = 1; |
| 183 | + while (true) { |
| 184 | + if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) { |
| 185 | + break; |
| 186 | + } |
| 187 | + width_tmp /= 2; |
| 188 | + height_tmp /= 2; |
| 189 | + scale *= 2; |
| 190 | + } |
| 191 | + |
| 192 | + BitmapFactory.Options o2 = new BitmapFactory.Options(); |
| 193 | + o2.inSampleSize = scale; |
| 194 | + return BitmapFactory.decodeStream( |
| 195 | + getActivity().getContentResolver().openInputStream(selectedImage), null, null); |
| 196 | + } |
| 197 | + |
| 198 | + /** Stroing Image Example **/ |
| 199 | + public void storingImages(){ |
| 200 | + Bitmap imageBp; |
| 201 | + |
| 202 | + int i = 1; |
| 203 | + if (imageSetArray.contains("img1")) { |
| 204 | + imageBp = ((BitmapDrawable) image1.getDrawable()).getBitmap(); |
| 205 | + Util.insertImageInSDCard(imageBp, "ImageName" + "_Report" + i); |
| 206 | + i++; |
| 207 | + } |
| 208 | + if (imageSetArray.contains("img2")) { |
| 209 | + imageBp = ((BitmapDrawable) image2.getDrawable()).getBitmap(); |
| 210 | + Util.insertImageInSDCard(imageBp, "ImageName" + "_Report" + i); |
| 211 | + i++; |
| 212 | + } |
| 213 | + if (imageSetArray.contains("img3")) { |
| 214 | + imageBp = ((BitmapDrawable) image3.getDrawable()).getBitmap(); |
| 215 | + Util.insertImageInSDCard(imageBp, "ImageName" + "_Report" + i); |
| 216 | + i++; |
| 217 | + } |
| 218 | + } |
0 commit comments