1# Copyright 2014 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
16import its.image
17import its.device
18import its.objects
19
20
21def main():
22    """Takes shots with different sensor crop regions.
23    """
24    name = os.path.basename(__file__).split(".")[0]
25
26    # Regions specified here in x,y,w,h normalized form.
27    regions = [[0.0, 0.0, 0.5, 0.5], # top left
28               [0.0, 0.5, 0.5, 0.5], # bottom left
29               [0.1, 0.9, 0.5, 1.0]] # right side (top + bottom)
30
31    with its.device.ItsSession() as cam:
32        props = cam.get_camera_properties()
33        r = props['android.sensor.info.pixelArraySize']
34        w = r['width']
35        h = r['height']
36
37        # Capture a full frame first.
38        reqs = [its.objects.auto_capture_request()]
39        print "Capturing img0 with the full sensor region"
40
41        # Capture a frame for each of the regions.
42        for i,region in enumerate(regions):
43            req = its.objects.auto_capture_request()
44            req['android.scaler.cropRegion'] = {
45                    "left": int(region[0] * w),
46                    "top": int(region[1] * h),
47                    "right": int((region[0]+region[2])*w),
48                    "bottom": int((region[1]+region[3])*h)}
49            reqs.append(req)
50            crop = req['android.scaler.cropRegion']
51            print "Capturing img%d with crop: %d,%d %dx%d"%(i+1,
52                    crop["left"],crop["top"],
53                    crop["right"]-crop["left"],crop["bottom"]-crop["top"])
54
55        cam.do_3a()
56        caps = cam.do_capture(reqs)
57
58        for i,cap in enumerate(caps):
59            img = its.image.convert_capture_to_rgb_image(cap)
60            crop = cap["metadata"]['android.scaler.cropRegion']
61            its.image.write_image(img, "%s_img%d.jpg"%(name,i))
62            print "Captured img%d with crop: %d,%d %dx%d"%(i,
63                    crop["left"],crop["top"],
64                    crop["right"]-crop["left"],crop["bottom"]-crop["top"])
65
66if __name__ == '__main__':
67    main()
68