一、OOM问题
在处理bitmap转byte数组时,可能会遇到OOM(Out Of Memory)问题,尤其是处理大图时。这是因为系统无法为处理大型bitmap所需的字节数组分配足够的内存。解决方案是使用inJustDecodeBounds属性来解决该问题。
/**
* bitmap转byte数组,解决OOM问题
* @param bitmap
* @return
*/
public static byte[] bitmap2Bytes(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] bytes = baos.toByteArray();
bitmap.recycle();
return bytes;
}
/**
* Bitmap转byte数组,inJustDecodeBounds属性解决OOM问题
* @param bitmap
* @return
*/
public static byte[] bitmap2BytesWithOOM(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.toByteArray().length, options);
int imageSize = options.outWidth * options.outHeight * 2;
options.inSampleSize = imageSize / (1024 * 1024);
options.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.toByteArray().length, options);
byte[] bytes = bitmap2Bytes(bmp);
bmp.recycle();
return bytes;
}
二、map转byte数组
在Java中,我们可以使用Map类型将逻辑数据转换为字节数组。此时,我们需要使用ByteArrayOutputStream和ObjectOutputStream类将Map转换为byte数组。
/**
* Map转byte数组
* @param map
* @return
* @throws IOException
*/
public static byte[] map2Bytes(Map map) throws IOException {
byte[] bytes;
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream(out);
objOut.writeObject(map);
objOut.flush();
bytes = out.toByteArray();
out.close();
objOut.close();
return bytes;
}
三、Byte数组转json
如果需要在网络传输中使用字节数组,我们可以将其转换为JSON格式。为此,我们需要使用JSONObject类和Base64类。
/**
* Byte数组转json
* @param bytes
* @return
*/
public static String bytes2Json(byte[] bytes) {
String jsonStr = "";
try {
jsonStr = Base64.encodeToString(bytes, Base64.DEFAULT);
jsonStr = JSONObject.quote(jsonStr);
jsonStr = jsonStr.replaceAll("\\\\\\\\", "\\\\");
} catch (Exception e) {
e.printStackTrace();
}
return jsonStr;
}
四、string转byte数组
如果需要将字符串转换为字节数组,我们可以使用getBytes()方法。需要注意的是,不同的编码方式可能会影响byte数组的大小和内容。
/**
* string转byte数组
* @param str
* @param charsetName
* @return
*/
public static byte[] string2Bytes(String str, String charsetName) {
try {
return str.getBytes(charsetName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
五、Byte数组转byte
如果需要将字节数组转换为单个字节,我们可以使用Java中的byte类型。在转换时需要注意字节数组的长度,否则可能会出现数组越界的问题。
/**
* Byte数组转byte
* @param bytes
* @return
*/
public static byte bytes2Byte(byte[] bytes) {
if (bytes.length != 1) {
return 0;
}
return bytes[0];
}
六、C# bitmap转byte数组
在C#语言中,我们也可以使用Bitmap类型将图片转换为字节数组。此时,我们使用MemoryStream类和BinaryWriter类将Bitmap转换为byte数组。
/**
* C# bitmap转byte数组
* @param bmp
* @param format
* @return
*/
public static byte[] getJpegBitmapData(Bitmap bmp, System.Drawing.Imaging.ImageFormat format) {
byte[] data = null;
using (MemoryStream ms = new MemoryStream()) {
using (BinaryWriter bw = new BinaryWriter(ms)) {
bmp.Save(ms, format);
data = ms.ToArray();
}
}
return data;
}
七、Byte数组转文件
如果需要将字节数组保存为文件,我们可以使用Java中的FileOutputStream类。在写入数据时需要注意一些异常处理。
/**
* Byte数组转文件
* @param bytes
* @param filePath
*/
public static void bytes2File(byte[] bytes, String filePath) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(filePath));
fos.write(bytes);
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
八、Byte数组转string
如果需要将字节数组转换为字符串,我们可以使用Java中的String类。在转换时需要注意字符串的编码方式。
/**
* Byte数组转string
* @param bytes
* @param charsetName
* @return
*/
public static String bytes2String(byte[] bytes, String charsetName) {
try {
return new String(bytes, charsetName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
九、bitmap转化成bayer图
将bitmap图像转换为Bayer格式的图像可以用于一些特殊的应用场景。我们可以使用Java中的getPixels方法将Bitmap图像的像素点取出,然后使用BitSet类将它们转换为Bayer格式的点。
/**
* bitmap转化成bayer图
* @param bmp
* @return
*/
public static byte[] bitmap2Bayer(Bitmap bmp) {
int width = bmp.getWidth();
int height = bmp.getHeight();
byte[] bayerData = new byte[width * height];
int[] pixelData = new int[width * height];
bmp.getPixels(pixelData, 0, width, 0, 0, width, height);
BitSet bits = new BitSet(width * height);
for (int i = 0; i < pixelData.length; i++) {
int gray = (int) ((0.3 * Color.red(pixelData[i]))
+ (0.59 * Color.green(pixelData[i]))
+ (0.11 * Color.blue(pixelData[i])));
bits.set(i, gray < 128);
}
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int index = i + j * width;
if ((i % 2 == 0) && (j % 2 == 0)) {
bayerData[index] = (byte) (bits.get(index) ? 1 : 0);
} else if ((i % 2 == 1) && (j % 2 == 0)) {
bayerData[index] = (byte) (bits.get(index) ? 2 : 0);
} else if ((i % 2 == 0) && (j % 2 == 1)) {
bayerData[index] = (byte) (bits.get(index) ? 0 : 2);
} else if ((i % 2 == 1) && (j % 2 == 1)) {
bayerData[index] = (byte) (bits.get(index) ? 3 : 0);
}
}
}
return bayerData;
}
原创文章,作者:BNPY,如若转载,请注明出处:https://www.506064.com/n/133597.html
微信扫一扫
支付宝扫一扫