View

Showing posts with label Image Conversion. Show all posts
Showing posts with label Image Conversion. Show all posts

Tuesday, 1 May 2012

How to use Image can convert into ByteArray

How to Image can convert into ByteArray
Here I used barcode image can convert into Byte Array and the Byte Array can reconvert into the original form.I used two Buttons,textview for values and one static image then ByteArrayOutputStream for conversion.
Here java code ,The class name as ImageConversion.java

public class ImageConvertScreen extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
  Button img_byte,byte_img;
  ImageView image;
  TextView value;
  public ByteArrayOutputStream bos;
  public Bitmap bm;
  public byte[] bitmapdata;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      
        image = (ImageView) findViewById(R.id.img_convert);
        value=(TextView)findViewById(R.id.convert_txt);
        img_byte =(Button)findViewById(R.id.img_byte);
        byte_img =(Button)findViewById(R.id.byte_img);
        img_byte.setOnClickListener(this);
        byte_img.setOnClickListener(this);
     
     
       bm = BitmapFactory.decodeResource(getResources(),R.drawable.barcode);
       bos = new ByteArrayOutputStream();
       bm.compress(Bitmap.CompressFormat.JPEG, 40 , bos);
      
      
    }
    public void onClick(View v) {
  if (v == img_byte) { 
   bitmapdata = bos.toByteArray();
      Log.w("Image Conversion", String.valueOf(bitmapdata.length));
      String converted_txt="";
         for (int i = 0; i < bitmapdata.length; i++) {
       Log.w("Image Conversion", String.valueOf(bitmapdata[i]));
          converted_txt=converted_txt+bitmapdata[i];
         }
         value.setText(converted_txt);
         value.setVisibility(View.VISIBLE);
         image.setVisibility(View.INVISIBLE);
  
  } else if (v==byte_img){
    bm = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);
        image.setImageBitmap(bm);
        image.setVisibility(View.VISIBLE);
        value.setText("");
        value.setVisibility(View.INVISIBLE);
        Log.w("Image Conversion", "converted");

  }
    }
   
}