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");
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/img_byte"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Image To Byte" android:layout_marginLeft="50dp"/>
<TextView
android:id="@+id/convert_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bytes"
android:textAppearance="?android:attr/textAppearanceLarge" android:visibility="invisible"/>
<ImageView
android:id="@+id/img_convert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:src="@drawable/barcode" android:layout_marginBottom="5dp"/>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/byte_img"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Byte To Image" android:layout_marginLeft="50dp"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
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");
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/img_byte"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Image To Byte" android:layout_marginLeft="50dp"/>
<TextView
android:id="@+id/convert_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bytes"
android:textAppearance="?android:attr/textAppearanceLarge" android:visibility="invisible"/>
<ImageView
android:id="@+id/img_convert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:src="@drawable/barcode" android:layout_marginBottom="5dp"/>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/byte_img"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Byte To Image" android:layout_marginLeft="50dp"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
this is possible when we have one image, if we have multiple images how can we convert at time into byte array...please tell me if u know answer
ReplyDeletethanks in advance....
Excellent post. I love your every post.
ReplyDeleteandroid applications
This comment has been removed by the author.
ReplyDeleteLog.w is (write)
ReplyDeletelog.i is (info)
log.e is (error)
you are great man !
ReplyDelete