File tree Expand file tree Collapse file tree 1 file changed +21
-2
lines changed
PiCowbell_Camera_Demos/JPEG_Capture Expand file tree Collapse file tree 1 file changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -76,8 +76,27 @@ def open_next_image():
7676 return open (filename , "wb" )
7777
7878cam .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+
81100jpeg = cam .capture (b )
82101
83102while True :
You can’t perform that action at this time.
0 commit comments