在我的android應用程序。我從代碼的jpeg圖像中獲得二進制代碼,如下所示。如何將二進制數據轉換爲圖像?
byte[] val = stream.toByteArray();
BigInteger bi = new BigInteger(val);
String s = bi.toString(2);
此字符串s打印圖像的二進制值。 我的問題是如何將這個二進制格式轉換成JPEG圖像?
在我的android應用程序。我從代碼的jpeg圖像中獲得二進制代碼,如下所示。如何將二進制數據轉換爲圖像?
byte[] val = stream.toByteArray();
BigInteger bi = new BigInteger(val);
String s = bi.toString(2);
此字符串s打印圖像的二進制值。 我的問題是如何將這個二進制格式轉換成JPEG圖像?
我不確定你想要什麼。
如果您想直接從流創建一個Bitmap
-instance你可以在ImageView
-instance以後使用BitmapFactory
和顯示Bitmap
:
Bitmap image = BitmapFactory.decodeStream(stream);
imageView.setImageBitmap(image);
如果你想你的字符串轉換用基數2表示回二進制數組,您也可以使用BigInteger
:
BigInteger bigInt = new BigInteger(s, 2);
byte[] binaryData = bigInt.toByteArray();
Klober你只能正確理解我的問題。這很好。非常感謝你的回覆 – 2012-01-06 12:39:55
Bitmap bmp=BitmapFactory.decodeByteArray(val, 0, val.length);
ImageView img = new ImageView(this);
img.setImageBitmap(bmp);
希望這有助於
編輯: 要在內部存儲器寫
FileOutputStream fout;
fout = openFileOutput("temp.jpg",Context.MODE_WORLD_WRITEABLE);
b1.compress(CompressFormat.JPEG, 100, fout);
要在外部存儲器寫
FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/temp.JPEG");
bm.compress(Bitmap.CompressFormat.JPEG,90, fout);
我將二進制值打印爲字符串。我想將二進制值轉換爲jpeg圖像。 – 2012-01-06 12:13:28
你想輸出爲SDcard上的.jpg嗎? – Jana 2012-01-06 12:17:14
是絕對:) – 2012-01-06 12:19:07
Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
savefile(bMap,"test.jpg");
private void savefile(Bitmap bt,String file)
{
try {
ContextWrapper context=this;
FileOutputStream stream =context.openFileOutput(file, 2);
BufferedOutputStream objectOut = null;
// FileOutputStream stream =(FileOutputStream) getAssets().open("temp.txt");
try {
objectOut = new BufferedOutputStream(stream);
bt.compress(Bitmap.CompressFormat.PNG, 100, objectOut);
objectOut.flush();
objectOut.close();
}
catch (Exception e) {
// TODO Auto-generated catch block
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
}
}
我用這個代碼在過去:
InputStream is = (InputStream)imageContent;
d = Drawable.createFromStream(is, "src");
只是將字節數組寫入文件 – epoch 2012-01-06 12:06:24