Thursday 19 March 2015

Pass Bitmap Data Between Activities in Android

Pass Bitamp as Extended Data

ByteArrayOutputStream bStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
    byte[] byteArray = bStream.toByteArray();

    Intent anotherIntent = new Intent(this, anotherActivity.class);
    anotherIntent.putExtra("image", byteArray);
    startActivity(anotherIntent);
    finish();

Retrieve Bitmap in Other Activity

Bitmap bmp;

    byte[] byteArray = getIntent().getByteArrayExtra("image");
    bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

Wednesday 18 March 2015

How To make Image Circle View

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {

    Bitmap sbmp;
    if(bmp.getWidth() != radius || bmp.getHeight() != radius)
        sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    else
        sbmp = bmp;
    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(),
            sbmp.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffa19774;

    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));

    canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,
            sbmp.getWidth() / 2+0.1f, paint);

    paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);


            return output;

}



result:











Saturday 14 March 2015

How to Convert Textview to Bitmap (View to Bitmap)


public static Bitmap loadBitmapFromView(View v) {
   
  Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height,Bitmap.Config.ARGB_8888);  
              
    Canvas c = new Canvas(b);

    v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);

    v.draw(c);
    return b;

}