2025-04-30
/*
	 * 將bitmap轉換為base64字節數組
	 */
	public byte[] Bitmap2Base64(Bitmap bitmap) {
		try {
			// 先將bitmap轉換為普通的字節數組
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
			out.flush();
			out.close();
			byte[] buffer = out.toByteArray();
			// 將普通字節數組轉換為base64數組
			byte[] encode = Base64.decode(buffer, Base64.DEFAULT);
			return encode;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/*
	 * 將base64字節數組轉換為bitmap
	 */
	public Bitmap Base642Bitmap(byte[] base64) {
		// 將base64字節數組轉換為普通的字節數組
		byte[] bytes = Base64.decode(base64, Base64.DEFAULT);
		// 用BitmapFactory創建bitmap
		return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
	}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *