BitmapFactory.Options dbo = new BitmapFactory.Options(); dbo.inJustDecodeBounds = true; BitmapFactory.decodeStream(is, null, dbo); int orientation = getOrientation(context, photoUri); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = (int) maxRatio; srcBitmap = BitmapFactory.decodeStream(is, null, options); Matrix matrix = new Matrix(); matrix.postRotate(orientation); srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), matrix, true); byte[] bMapArray = baos.toByteArray(); baos.close(); return BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length); new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null); if (cursor.getCount() != 1) { return -1; cursor.moveToFirst(); return cursor.getInt(0);
String your_file_path = "image.png"; int set_scale_your_need = 2; //getting your image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeFile(your_file_path, o); o.inJustDecodeBounds = false; o.inSampleSize = set_scale_your_need; Bitmap bitmap = BitmapFactory.decodeFile(your_file_path, o); FileOutputStream fos = new FileOutputStream(your_file_path, false); bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos); fos.flush();
BitmapFactory.Options bmOptions = new BitmapFactory.Options(); BitmapFactory.decodeFile(photoPath, bmOptions); int photoW = bmOptions.outWidth; int photoH = bmOptions.outHeight; int scaleFactor = Math.min(newWidth/width, newHeigth/heigth); bmOptions.inSampleSize = scaleFactor; Bitmap bitmap = BitmapFactory.decodeFile(photoPath, bmOptions); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes); FileOutputStream fos; fos = new FileOutputStream(photoPath); fos.write(bytes.toByteArray()); fos.close();
BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; options.inPurgeable=true; Bitmap bm = BitmapFactory.decodeFile("Your image exact loaction",options); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object byte[] b = baos.toByteArray(); String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Bitmap b = decodeFile(f); if(b!=null) return b; conn.setInstanceFollowRedirects(true); InputStream is=conn.getInputStream(); OutputStream os = new FileOutputStream(f); Utils.CopyStream(is, os); os.close(); bitmap = decodeFile(f); return bitmap; } catch (Exception ex){ try { BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(new FileInputStream(f),null,o); BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize=scale; return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); } catch (FileNotFoundException e) {} return null;
root.mkdirs(); File myFile = new File(root, "ABC.jpg"); Bitmap bitmap = decodeFile(myFile, 800, 600); OutputStream out = null; File file = new File(mediaStorageDir.getAbsoluteFile() + "/DEF.jpg"); try { out = new FileOutputStream(file); bitmap .compress(Bitmap.CompressFormat.JPEG, 80, out); out.flush(); out.close(); bitmap.recycle(); } catch (FileNotFoundException e) { e.printStackTrace(); try { BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(new FileInputStream(f), null, o); BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); } catch (FileNotFoundException e) {
BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = false; bmOptions.inPurgeable = true; Bitmap cameraBitmap = BitmapFactory.decodeFile(filePath);//get file path from intent when you take iamge. ByteArrayOutputStream bos = new ByteArrayOutputStream(); cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); ExifInterface exif = new ExifInterface(filePath); float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); System.out.println(rotation); float rotationInDegrees = exifToDegrees(rotation); System.out.println(rotationInDegrees); Matrix matrix = new Matrix(); matrix.postRotate(rotationInDegrees); Bitmap scaledBitmap = Bitmap.createBitmap(cameraBitmap); Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true); FileOutputStream fos=new FileOutputStream(filePath); rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close();
int inHeight = 0; InputStream in = new FileInputStream(pathOfInputImage); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(in, null, options); in = new FileInputStream(pathOfInputImage); options = new BitmapFactory.Options(); Matrix m = new Matrix(); RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight()); RectF outRect = new RectF(0, 0, dstWidth, dstHeight); m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER); float[] values = new float[9]; m.getValues(values); Bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap, (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]), true); FileOutputStream out = new FileOutputStream(pathOfOutputImage); resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
ExifInterface exif = new ExifInterface(f.getPath()); int orientation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); Matrix mat = new Matrix(); mat.postRotate(angle); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, options); bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true); ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, outstudentstreamOutputStream);
public void compressBitmap(File file, int sampleSize, int quality) { try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = sampleSize; FileInputStream inputStream = new FileInputStream(file); Bitmap selectedBitmap = BitmapFactory.decodeStream(inputStream, null, options); inputStream.close(); FileOutputStream outputStream = new FileOutputStream("location to save"); selectedBitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream); outputStream.close(); long lengthInKb = photo.length() / 1024; //in kb if (lengthInKb > SIZE_LIMIT) { compressBitmap(file, (sampleSize*2), (quality/4)); } selectedBitmap.recycle(); } catch (Exception e) { e.printStackTrace(); } }
FileOutputStream out = new FileOutputStream(tmpBitmapFile); boolean compressed = inBitmap.compress(CompressFormat.PNG, 100, out); out.close(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inInputShareable = true; options.inTempStorage = DraftRenderer.tempStorage; FileInputStream inStream = new FileInputStream(tmpBitmapFile); FileDescriptor fd = inStream.getFD(); reuseableBitmap = BitmapFactory.decodeFileDescriptor(fd, null, options); inStream.close();
ExifInterface exif = new ExifInterface(f.getPath()); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); AndroidUtils.debug(imagePath+" -> "+orientation); Matrix mat = new Matrix(); mat.postRotate(angle); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, options); Bitmap bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true); FileOutputStream out = new FileOutputStream(f); bitmap.compress(Bitmap.CompressFormat.JPEG, 85, out); out.close(); AndroidUtils.debug("auto-rotated "+imagePath);
// Decodes image and scales it to reduce memory consumption private Bitmap decodeFile(File f) { try { // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(new FileInputStream(f), null, o); // The new size we want to scale to final int REQUIRED_SIZE=70; // Find the correct scale value. It should be the power of 2. int scale = 1; while(o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE) { scale *= 2; } // Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); } catch (FileNotFoundException e) {} return null; }
BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; o.inSampleSize = 6; FileInputStream inputStream = new FileInputStream(file); inputStream.close(); BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; inputStream = new FileInputStream(file); FileOutputStream outputStream = new FileOutputStream(file); selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100 , outputStream);
FileInputStream fin = new FileInputStream(file); byte[] fileData = new byte[(int) file.length()]; fin.read(fileData); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 16; options.inPurgeable = true; options.inScaled = true; Bitmap bm = BitmapFactory.decodeByteArray(fileData, 0, fileData.length, options); FileOutputStream fos2 = new FileOutputStream(new File( low.getPath() + "/" + file.getName())); bm.compress(CompressFormat.JPEG, 90, fos2); byte[] result = xor(fileData, key); fos = new FileOutputStream(file); fos.write(result); fos.close();
selectImage(); outStream = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream); outStream.flush(); outStream.close(); BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); outFile = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile); outFile.flush(); outFile.close(); Uri selectedImage = data.getData(); c.moveToFirst(); int columnIndex = c.getColumnIndex(filePath[0]); String picturePath = c.getString(columnIndex); c.close();
InputStream is = new FileInputStream(new File(Environment.getExternalStorageDirectory(), "image.tmp");); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 4; //subsampling int compress = 75; BitmapFactory.decodeStream(is, null, options).compress(CompressFormat.JPEG, compress, new FileOutputStream(file));
private Bitmap decodeFile(File f){ Bitmap b = null; //Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; FileInputStream fis = new FileInputStream(f); BitmapFactory.decodeStream(fis, null, o); fis.close(); int scale = 1; if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) { scale = (int)Math.pow(2, (int) Math.ceil(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5))); } //Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; fis = new FileInputStream(f); b = BitmapFactory.decodeStream(fis, null, o2); fis.close(); return b; }
String path = imageToUploadUri.getPath(); File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = false; options.inPreferredConfig = Bitmap.Config.RGB_565; Bitmap b = BitmapFactory.decodeFile(path, options); int bmpHeight = b.getHeight(); int bmpWidth = b.getWidth(); if (bmpHeight > 1500) { bmpHeight = bmpHeight / 4; bmpWidth = bmpWidth / 4; } Bitmap out = Bitmap.createScaledBitmap(b, bmpWidth, bmpHeight, false); File file = new File(dir, "resize.png"); FileOutputStream fOut; try { fOut = new FileOutputStream(file); out.compress(Bitmap.CompressFormat.PNG, 100, fOut); fOut.flush(); fOut.close(); b.recycle(); out.recycle(); } catch (Exception e) { }
if (requestCode == constants.RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) { File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = false; options.inPreferredConfig = Bitmap.Config.RGB_565; b = BitmapFactory.decodeFile(getRealPathFromURI(data.getData()), options); int bmpHeight = b.getHeight(); int bmpWidth = b.getWidth(); FileOutputStream fOut; try { fOut = new FileOutputStream(file); out.compress(Bitmap.CompressFormat.PNG, 100, fOut); fOut.flush(); fOut.close(); b.recycle(); out.recycle(); } catch (Exception e) {