Bitmap compress - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Bitmap compress method.

This code example shows how to use the following methods:compress, createBitmap, getHeight, getWidth
 
	private static int MAX_IMAGE_DIMENSION = 720;
	 
	public byte[] scaleImage(Context context, Bitmap bitmap, Bitmap.CompressFormat format) {
		 
		int width = bitmap.getWidth();
		 
		if(width > MAX_IMAGE_DIMENSION) {
			float ratio = (float) MAX_IMAGE_DIMENSION / (float) width;
			 
			width = MAX_IMAGE_DIMENSION;
			int height = (int)((float)bitmap.getHeight() * ratio);
			bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height);
		} 
 
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		bitmap.compress(format, 100, stream);
		return stream.toByteArray();
	} 
 

7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compress, getHeight, getWidth
            mEntry.put(Photo.PHOTO, (byte[])null);
            resetDefault(); 
            return; 
        } 
 
        final int size = photo.getWidth() * photo.getHeight() * 4;
        final ByteArrayOutputStream out = new ByteArrayOutputStream(size);
 
        try { 
            photo.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
 
            mEntry.put(Photo.PHOTO, out.toByteArray());
            setImageBitmap(photo);
            setEnabled(true);
            mHasSetPhoto = true;
            mEntry.setFromTemplate(false);
 
            // When the user chooses a new photo mark it as super primary 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compress, recycle
		 
		try { 
			ref = TiDrawableReference.fromBlob(getActivity(), blob);
			image = ref.getBitmap();
			bos = new ByteArrayOutputStream();
			if (image.compress(CompressFormat.JPEG, (int)(compressionQuality * 100), bos)) {
				byte[] data = bos.toByteArray();
				BitmapFactory.Options bfOptions = new BitmapFactory.Options();
				bfOptions.inPurgeable = true;
				bfOptions.inInputShareable = true;
				result = TiBlob.blobFromData(data, "image/jpeg");
				coerceDimensionsIntoBlob(image, result);
			} 
		} catch (OutOfMemoryError e) {
			Log.e(LCAT, "Received an OutOfMemoryError! The image is too big to compress all at once. Consider using the \"compressToFile\" method instead.");
		}  
		finally { 
            // [MOD-309] Free up memory to work around issue in Android 
			if (image != null) {
				image.recycle();

7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compress, getHeight, getWidth
            boolean success = false;
            try { 
                // Try go guesstimate how much space the icon will take when 
                // serialized to avoid unnecessary allocations/copies during 
                // the write. 
                int size = bitmap.getWidth() * bitmap.getHeight() * 4;
                ByteArrayOutputStream out = new ByteArrayOutputStream(size);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.flush();
                out.close();
 
                ContentValues values = new ContentValues();
                values.put(PhotoDatabaseHelper.FIELD_APPWIDGET_ID, appWidgetId);
                values.put(PhotoDatabaseHelper.FIELD_PHOTO_BLOB,
                           out.toByteArray());
 
                SQLiteDatabase db = getWritableDatabase();
                db.insertOrThrow(PhotoDatabaseHelper.TABLE_PHOTOS, null,
                                 values);
 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compress
                Bitmap b = mTextureView.getBitmap(800, 800);
                BufferedOutputStream out = null;
                try { 
                    File dump = new File(Environment.getExternalStorageDirectory(), "out.png");
                    out = new BufferedOutputStream(new FileOutputStream(dump));
                    b.compress(Bitmap.CompressFormat.PNG, 100, out);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally { 
                    if (out != null) try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } 
                } 
            } 
        }); 
 
        Button button = new Button(this);
        button.setText("Remove/Add");
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compress, getHeight, getWidth
    /** 
     * Creates a byte[] containing the PNG-compressed bitmap, or null if 
     * something goes wrong. 
     */ 
    public static byte[] compressBitmap(Bitmap bitmap) {
        final int size = bitmap.getWidth() * bitmap.getHeight() * 4;
        final ByteArrayOutputStream out = new ByteArrayOutputStream(size);
        try { 
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
            return out.toByteArray();
        } catch (IOException e) {
            Log.w(TAG, "Unable to serialize photo: " + e.toString());
            return null; 
        } 
    } 
 
    public static void addCropExtras(Intent intent, int photoSize) {
        intent.putExtra("crop", "true");
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compress
 
        try { 
            File cacheFile = getCacheFile(cacheKey);
            out = new BufferedOutputStream(new FileOutputStream(cacheFile));
 
            boolean didCompress = data.compress(compressFormat, quality, out);
            if (!didCompress) {
                throw new CacheSavingException(String.format("Could not compress bitmap for path: %s", getCacheFile(cacheKey).getAbsolutePath()));
            } 
 
            return data;
        } catch (IOException e) {
            throw new CacheSavingException(e);
        } finally { 
            IOUtils.closeQuietly(out);
        } 
    } 
 
    public BitmapFactory.Options getDecodingOptions() {
        return decodingOptions;
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compress, getHeight, getWidth
    /** 
     * Helper method to compress the given bitmap as a JPEG and return the resulting byte array. 
     */ 
    private byte[] getCompressedBytes(Bitmap b, int quality) throws IOException {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final boolean compressed = b.compress(Bitmap.CompressFormat.JPEG, quality, baos);
        baos.flush();
        baos.close();
        byte[] result = baos.toByteArray();
 
        if (!compressed) {
            throw new IOException("Unable to compress image");
        } 
        return result;
    } 
 
    /** 
     * Retrieves the uncompressed display photo. 
     */ 
    public Bitmap getDisplayPhoto() {

6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compress, isRecycled, recycle
		return output;
	} 
 
	public static boolean saveBitmapToFile(final Bitmap bitmap, String savePath) {
		boolean result = false;
		if (bitmap != null && !bitmap.isRecycled()) {
			File myDrawFile = new File(savePath);
			try { 
				BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myDrawFile));
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
				bos.flush();
				bos.close();
				result = true;
			} catch (Exception e) {
				Log.e("saveBitmapToFile", e);
			} finally { 
				if (bitmap != null) {
					bitmap.recycle();
				} 
			} 

7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compress, createBitmap, getHeight, getWidth
			if (bitmap == null) return null;
 
			bitmap = rotateByExif(bitmap, fileName);
			if (thumbNail != null) {
				try { 
					bitmap.compress(CompressFormat.JPEG, 85, new FileOutputStream(thumbNail));
				} catch (FileNotFoundException e) {
				} 
			} 
			return bitmap;
		} catch (FileNotFoundException e) {
			Log.d("thumbnail", "FileNotFoundException");
			return null; 
		} 
	} 
	static public Bitmap rotate(Bitmap image, int rotation) {
		if (rotation == 0) return image;
		Matrix matrix = new Matrix();
		matrix.postRotate(rotation);
		return Bitmap.createBitmap(image, 0, 0, 

7
CodeRank
Experience pair programming with AI  Get Codota for Java

Related Bitmap compress Questions & Answers:

compress Method Overview
Write a compressed version of the bitmap to the specified outputstream.
See Code Examples for other Android Bitmap Methods: