从创建Bitmap类.BMP图像文件 [英] creating .bmp image file from Bitmap class

查看:175
本文介绍了从创建Bitmap类.BMP图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个使用在客户端接收图像插槽和存储图像数据以位图类的应用程序....

谁能告诉我如何创建一个名为 myimage.png myimage.bmp 此位图对象

 字符串的base64 code = dataInputStream.readUTF();
字节[]德codedString = NULL;
德codedString = Base64.de code(BASE64 code);
位图位图= BitmapFactory.de codeByteArray的(德codedString,0,德codedString.length);


解决方案

尝试以下code图像保存为 PNG 格式

  {尝试
   FileOutputStream中出=新的FileOutputStream(文件名);
   bitmap.com preSS(Bitmap.Com pressFormat.PNG,100,出);
}赶上(例外五){
   e.printStackTrace();
}
了out.flush();
out.close();

在这里,100是质量的COM pression保存。您可以在0到100下通过的任何数字,质量差的大小为下降。

注意

您需要采取许可,Android清单文件。

 <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>

修改

要保存图像的 .BMP 格式的 Android的位图的Util 会帮助你。它有非常简单的实现。

 字符串sdcardBmpPath = Environment.getExternalStorageDirectory()getAbsolutePath()+/sample_text.bmp。
位图testBitmap = BitmapFactory.de codeResource(getResources(),R.drawable.sample_text);
AndroidBmpUtil bmpUtil =新AndroidBmpUtil();
布尔isSaveResult = bmpUtil.save(testBitmap,sdcardBmpPath);

I've created an application that uses sockets in which the client receives the image and stores the data of the image in Bitmap class....

Can anyone please tell me how to create a file named myimage.png or myimage.bmp from this Bitmap object

String base64Code = dataInputStream.readUTF();
byte[] decodedString = null;
decodedString = Base64.decode(base64Code);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0,decodedString.length);

解决方案

Try following code to save image as PNG format

try {
   FileOutputStream out = new FileOutputStream(filename);
   bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
   e.printStackTrace();
}
out.flush();
out.close();

Here, 100 is quality to save in Compression. You can pass anything between 0 to 100. Lower the digit, poor quality with decreased size.

Note

You need to take permission in Android Manifest file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Edit

To save your image to .BMP format, Android Bitmap Util will help you. It has very simple implementation.

String sdcardBmpPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/sample_text.bmp";
Bitmap testBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample_text);
AndroidBmpUtil bmpUtil = new AndroidBmpUtil();
boolean isSaveResult = bmpUtil.save(testBitmap, sdcardBmpPath);

这篇关于从创建Bitmap类.BMP图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆