Skip to content

Commit 6bce301

Browse files
authored
Merge pull request #3018 from casten/patch-2
Detect max quality level dynamically.
2 parents c9b7193 + 8d0ef9d commit 6bce301

File tree

1 file changed

+21
-2
lines changed
  • PiCowbell_Camera_Demos/JPEG_Capture

1 file changed

+21
-2
lines changed

Diff for: PiCowbell_Camera_Demos/JPEG_Capture/code.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,27 @@ def open_next_image():
7676
return open(filename, "wb")
7777

7878
cam.colorspace = adafruit_ov5640.OV5640_COLOR_JPEG
79-
cam.quality = 5
80-
b = bytearray(cam.capture_buffer_size)
79+
80+
# Different platforms have different amounts of memory available.
81+
# Typically a Pico 2 can handle quality = 2 and a Pico can handle quality = 5.
82+
# Rather than detect and select sizes, let's try to detect the best dynamically
83+
# for broader platform support.
84+
# Start with the highest quality setting and attempt to allocate a buffer
85+
# of the necessary size. If it fails, try the next lowest.
86+
b = None
87+
for quality in range(2,55): #valid range is 2 to 54 inclusive
88+
try:
89+
cam.quality = quality
90+
print(f"Attempting to use quality {quality}.")
91+
b = bytearray(cam.capture_buffer_size)
92+
print(f"Quality {quality} successfully selected.")
93+
break
94+
except MemoryError:
95+
print(f"Quality {quality} was too big. Trying next lowest.")
96+
97+
if b is None:
98+
print("There wasn't enough system memory to allocate the lowest quality buffer.")
99+
81100
jpeg = cam.capture(b)
82101

83102
while True:

0 commit comments

Comments
 (0)