一、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/zh-tw/n/133597.html