Compressing converting a bitmap to PNG format Android example

public class MainActivity extends Activity {
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}
 
	private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,
			int quality) {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		src.compress(format, quality, os);
 
		byte[] array = os.toByteArray();
		return BitmapFactory.decodeByteArray(array, 0, array.length);
	}
 
	private static class SampleView extends View {
 
		// CONSTRUCTOR
		public SampleView(Context context) {
			super(context);
			setFocusable(true);
 
		}
		@Override
		protected void onDraw(Canvas canvas) {
			Paint paint = new Paint();
 
			canvas.drawColor(Color.YELLOW);
 
 
			Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.flower_blue);
 
			Bitmap bPNGcompress = codec(bmp, Bitmap.CompressFormat.PNG, 0);
 
		   int h = bmp.getHeight();
 
           canvas.drawBitmap(bmp, 10,10, paint);
           canvas.drawBitmap(bPNGcompress, 10,10 + h + 10, paint);
 
		}
 
	}
}
This entry was posted in Android Examples Code, Bitmap, drawing and tagged , , . Bookmark the permalink.


Comments are closed.

17,477 views