2013-12-20 27 views
1

我有一个ImageView,我正在为其分配使用zxing库生成的qr代码。转换ImageView的字节android?

<ImageView 
     android:id="@+id/qrcode" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="15dp" 
     android:layout_marginTop="86dp" /> 

这是我的imageview,我想先转换这个ImageView,然后再转换成bytearray。我可以做到这一点吗?

回答

8

如何查看转换成位图

ImageView view = (ImageView)findViewById(R.id.qrcode); 

view.setDrawingCacheEnabled(true); 

view.buildDrawingCache(); 

Bitmap bm = view.getDrawingCache(); 

位图转换成的ByteArray

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bm.compress(Bitmap.CompressFormat.PNG, 100, stream); 
byte[] byteArray = stream.toByteArray(); 
3
image = (ImageView)findViewById(R.id.qrcode); 
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap(); 
ByteArrayOutputStream stream=new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); 
byte[] image=stream.toByteArray(); 
System.out.println("byte array:"+image); 

String img_str = Base64.encodeToString(image, 0); 
System.out.println("string:"+img_str); 
0

设置图像 - >image=(Imageview)findViewById(R.id.imageview1);

转换成字节阵列

Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.images); 
     ByteArrayOutputStream stream=new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); 
     byte[] image=stream.toByteArray(); 
     System.out.println("byte array:"+image); 
     Log.d("array",image) 
     String img_str = Base64.encodeToString(image, 0); 
     Log.d("array_Str",image_str) 
     System.out.println("string:"+img_str);