framebuf --- 影格緩衝區操作¶
framebuf 模組提供一個小型、免配置記憶體的像素緩衝區,並具備基本的繪製操作。它的用途是驅動外接顯示器(OLED、LCD、電子紙等)。
備註
若要對擷取的影格進行影像處理工作,請改用 OpenMV 功能豐富許多的 image.Image 類別——相較於 framebuf,它提供更多的繪製基本元件、色彩轉換與分析功能。
class FrameBuffer¶
A FrameBuffer wraps a user-supplied buffer-protocol object
(typically a bytearray) and exposes methods to draw pixels,
lines, rectangles, ellipses, polygons, text and other FrameBuffers into it.
範例::
import framebuf
# FrameBuffer needs 2 bytes for every RGB565 pixel.
fbuf = framebuf.FrameBuffer(bytearray(100 * 10 * 2), 100, 10, framebuf.RGB565)
fbuf.fill(0)
fbuf.text("MicroPython!", 0, 0, 0xffff)
fbuf.hline(0, 9, 96, 0xffff)
建構函式¶
- class framebuf.FrameBuffer(buffer: Any, width: int, height: int, format: int, stride: int | None = None, /)¶
建構一個
FrameBuffer物件。buffer-- 任何支援緩衝區協定的物件;必須足夠大,能以所選的format容納stride * height個像素。width-- 影格緩衝區的寬度(以像素為單位)。height-- 影格緩衝區的高度(以像素為單位)。format-- 像素格式;為下方 常數 所列常數之一。此格式同時決定buffer中每個像素的大小,以及傳遞給任何繪製方法的色彩整數c如何被解讀。stride-- 每個水平列的像素數量,包含任何填補。預設為width。設定此值可使用較大緩衝區的子區域。
傳入過小的
buffer或無效的尺寸會產生未定義的結果——建構函式並不會驗證每一種組合。
繪製方法¶
- FrameBuffer.fill_rect(x: int, y: int, w: int, h: int, c: int) None¶
在
(x, y)處以色彩c填滿一個wxh的矩形。等同於f=True的rect()。
- FrameBuffer.pixel(x: int, y: int, c: int | None = None) int | None¶
不帶
c引數時,回傳(x, y)處像素的色彩值。給定c時,將該像素設定為色彩c。
- FrameBuffer.hline(x: int, y: int, w: int, c: int) None¶
- FrameBuffer.vline(x: int, y: int, h: int, c: int) None¶
- FrameBuffer.line(x1: int, y1: int, x2: int, y2: int, c: int) None¶
以色彩
c繪製 1 像素粗的線條。hline()與vline()繪製指定長度的水平/垂直線;line()則在兩個任意點之間繪製線條。
- FrameBuffer.rect(x: int, y: int, w: int, h: int, c: int, f: bool = False) None¶
在
(x, y)處繪製一個大小為wxh、色彩為c的矩形。若f為True則填滿矩形;否則僅繪製 1 像素的外框。
- FrameBuffer.ellipse(x: int, y: int, xr: int, yr: int, c: int, f: bool = False, m: int = 0) None¶
繪製一個以
(x, y)為中心、x 半徑為xr、y 半徑為yr、色彩為c的橢圓。半徑相等時會產生圓形。f=True會填滿圖形,而非僅繪製外框。m是一個位元遮罩,將繪製限制在特定象限(從右上方起逆時針編號):位元
象限
區域
bit 0
Q1
右上
bit 1
Q2
左上
bit 2
Q3
左下
bit 3
Q4
右下
預設的
m=0會繪製全部四個象限。
- FrameBuffer.poly(x: int, y: int, coords: Any, c: int, f: bool = False) None¶
在位移
(x, y)處以色彩c繪製任意封閉多邊形(凸形或凹形)。coords必須是排列為array('h', [x0, y0, x1, y1, ..., xn, yn])的有號 16 位元整數array。f=True會填滿多邊形,而非僅繪製外框。
- FrameBuffer.text(s: str, x: int, y: int, c: int = 1) None¶
以色彩
c繪製字串s,其左上角位於(x, y)。內建字型固定為 8x8 像素且無法變更。c預設為1。
- FrameBuffer.scroll(xstep: int, ystep: int) None¶
將緩衝區內容位移
(xstep, ystep)。從緩衝區外移入的像素不會被清除,因此先前內容的「殘影」可能殘留在尾端邊緣。
- FrameBuffer.blit(fbuf: FrameBuffer | Tuple, x: int, y: int, key: int = -1, palette: FrameBuffer | None = None) None¶
將另一個影格緩衝區
fbuf繪製到此緩衝區之上,其左上角位於(x, y)。若給定
key,任何符合該色彩值的來源像素會被視為透明而不繪製。當提供palette時,比較是針對調色盤輸出進行,而非原始的fbuf值。fbuf可以是FrameBuffer實例,或符合建構函式簽章的 tuple/list::(buffer, width, height, format) (buffer, width, height, format, stride)
當來源為 tuple/list 時,
buffer可以是唯讀的。palette允許在不同格式的緩衝區之間進行 blit——例如將單色字符繪製到 RGB565 緩衝區中。它是一個格式與目的端相符、高度為 1、寬度等於來源色彩數量(N 位元每像素的來源為2**N)的FrameBuffer。來源像素值i在繪製前會被替換為palette[i, 0]處的色彩。
常數¶
建構函式接受以下 format 值。「每像素位元組數」欄是調整後備緩衝區大小時所需的乘數。
常數 |
每像素位元組數 |
像素配置 |
|---|---|---|
|
0.125 |
單色(1 位元)。每個位元組保存 8 個垂直堆疊的像素,bit 0 最接近頂端。每列 8 個像素由左至右橫越緩衝區推進,接著換到下一個 8 像素列。 |
|
0.125 |
單色(1 位元)。每個位元組保存 8 個水平像素,bit 7 為最左。每列一次垂直推進一個像素。 |
|
0.125 |
單色(1 位元)。與 |
|
0.25 |
2 位元灰階(4 個層級),以最高有效位元優先水平打包。 |
|
0.5 |
4 位元灰階(16 個層級),以最高有效半位元組優先水平打包。 |
|
1 |
8 位元灰階(256 個層級)。 |
|
2 |
16 位元 RGB,含 5 個紅色、6 個綠色與 5 個藍色位元。 |
framebuf.MVLSB 是 framebuf.MONO_VLSB 已棄用的別名;新程式碼中請優先使用後者。
舊版建構函式¶
- framebuf.FrameBuffer1(buffer: Any, width: int, height: int, stride: int | None = None, /) FrameBuffer¶
FrameBuffer(buffer, width, height, framebuf.MONO_VLSB, stride)已棄用的捷徑。為向後相容而保留;請改用完整的FrameBuffer建構函式。