| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This example makes use of mojo:camera_roll which is available only when | 5 // This example makes use of mojo:camera which is available only when |
| 6 // running on Android. | 6 // running on Android. It repeatedly captures camera video frame images |
| 7 // and displays it in a mojo view. |
| 7 // | 8 // |
| 8 // Example usage: | 9 // Example usage: |
| 9 // pub get | 10 // pub get |
| 10 // pub run sky_tools build | 11 // pub run sky_tools build |
| 11 // pub run sky_tools run_mojo --mojo-path=../../.. --android | 12 // pub run sky_tools run_mojo --mojo-path=../../.. --android |
| 12 | 13 |
| 13 import 'dart:sky'; | 14 import 'dart:sky'; |
| 14 import 'dart:typed_data'; | 15 import 'dart:typed_data'; |
| 15 | 16 |
| 16 import 'package:mojo_services/mojo/camera_roll.mojom.dart'; | 17 import 'package:mojo_services/mojo/camera.mojom.dart'; |
| 17 import 'package:sky/services.dart'; | 18 import 'package:sky/services.dart'; |
| 18 | 19 |
| 19 final CameraRollServiceProxy cameraRoll = new CameraRollServiceProxy.unbound(); | 20 Image image = null; |
| 20 Image currentImage; | 21 final CameraServiceProxy camera = new CameraServiceProxy.unbound(); |
| 21 int photoIndex = 0; | |
| 22 | 22 |
| 23 Picture paint(Rect paintBounds) { | 23 Picture paint(Rect paintBounds) { |
| 24 PictureRecorder recorder = new PictureRecorder(); | 24 PictureRecorder recorder = new PictureRecorder(); |
| 25 Canvas canvas = new Canvas(recorder, paintBounds); | 25 if (image != null) { |
| 26 Paint paint = new Paint()..color = const Color.fromARGB(255, 0, 255, 0); | 26 Canvas canvas = new Canvas(recorder, paintBounds); |
| 27 if (currentImage != null) { | 27 canvas.translate(paintBounds.width / 2.0, paintBounds.height / 2.0); |
| 28 canvas.drawImage(currentImage, new Point(0.0, 0.0), paint); | 28 canvas.scale(0.3, 0.3); |
| 29 Paint paint = new Paint()..color = const Color.fromARGB(255, 0, 255, 0); |
| 30 canvas.drawImage(image, new Point(-image.width / 2.0, -image.height / 2.0),
paint); |
| 29 } | 31 } |
| 30 return recorder.endRecording(); | 32 return recorder.endRecording(); |
| 31 } | 33 } |
| 32 | 34 |
| 33 Scene composite(Picture picture, Rect paintBounds) { | 35 Scene composite(Picture picture, Rect paintBounds) { |
| 34 final double devicePixelRatio = view.devicePixelRatio; | 36 final double devicePixelRatio = view.devicePixelRatio; |
| 35 Rect sceneBounds = new Rect.fromLTWH( | 37 Rect sceneBounds = new Rect.fromLTWH( |
| 36 0.0, 0.0, view.width * devicePixelRatio, view.height * devicePixelRatio); | 38 0.0, 0.0, view.width * devicePixelRatio, view.height * devicePixelRatio); |
| 37 Float32List deviceTransform = new Float32List(16) | 39 Float32List deviceTransform = new Float32List(16) |
| 38 ..[0] = devicePixelRatio | 40 ..[0] = devicePixelRatio |
| 39 ..[5] = devicePixelRatio | 41 ..[5] = devicePixelRatio |
| 40 ..[10] = 1.0 | 42 ..[10] = 1.0 |
| 41 ..[15] = 1.0; | 43 ..[15] = 1.0; |
| 42 SceneBuilder sceneBuilder = new SceneBuilder(sceneBounds) | 44 SceneBuilder sceneBuilder = new SceneBuilder(sceneBounds) |
| 43 ..pushTransform(deviceTransform) | 45 ..pushTransform(deviceTransform) |
| 44 ..addPicture(Offset.zero, picture, paintBounds) | 46 ..addPicture(Offset.zero, picture, paintBounds) |
| 45 ..pop(); | 47 ..pop(); |
| 46 return sceneBuilder.build(); | 48 return sceneBuilder.build(); |
| 47 } | 49 } |
| 48 | 50 |
| 49 void beginFrame(double timeStamp) { | 51 void beginFrame(double timeStamp) { |
| 50 Rect paintBounds = new Rect.fromLTWH(0.0, 0.0, view.width, view.height); | 52 Rect paintBounds = new Rect.fromLTWH(0.0, 0.0, view.width, view.height); |
| 51 Picture picture = paint(paintBounds); | 53 Picture picture = paint(paintBounds); |
| 52 Scene scene = composite(picture, paintBounds); | 54 Scene scene = composite(picture, paintBounds); |
| 53 view.scene = scene; | 55 view.scene = scene; |
| 54 } | 56 } |
| 55 | 57 |
| 56 void getPhoto() { | 58 void drawNextPhoto() { |
| 57 var future = cameraRoll.ptr.getPhoto(photoIndex); | 59 var future = camera.ptr.getLatestFrame(); |
| 58 future.then((response) { | 60 future.then((response) { |
| 59 if (response.photo == null) { | 61 if (response.content == null) { |
| 60 print("Photo $photoIndex not found, returning to the first photo."); | 62 drawNextPhoto(); |
| 61 cameraRoll.ptr.update(); | |
| 62 photoIndex = 0; | |
| 63 getPhoto(); | |
| 64 return; | 63 return; |
| 65 } | 64 } |
| 66 | 65 new ImageDecoder(response.content.handle.h, (frame) { |
| 67 new ImageDecoder(response.photo.content.handle.h, (image) { | 66 if (frame != null) { |
| 68 if (image != null) { | 67 image = frame; |
| 69 currentImage = image; | |
| 70 print("view.scheduleFrame"); | |
| 71 view.scheduleFrame(); | 68 view.scheduleFrame(); |
| 69 drawNextPhoto(); |
| 72 } | 70 } |
| 73 }); | 71 }); |
| 74 }); | 72 }); |
| 75 } | 73 } |
| 76 | 74 |
| 77 bool handleEvent(Event event) { | |
| 78 if (event.type == 'pointerdown') { | |
| 79 return true; | |
| 80 } | |
| 81 | |
| 82 if (event.type == 'pointerup') { | |
| 83 photoIndex++; | |
| 84 getPhoto(); | |
| 85 return true; | |
| 86 } | |
| 87 | |
| 88 return false; | |
| 89 } | |
| 90 | |
| 91 void main() { | 75 void main() { |
| 92 embedder.connectToService("mojo:camera_roll", cameraRoll); | |
| 93 view.setFrameCallback(beginFrame); | 76 view.setFrameCallback(beginFrame); |
| 94 view.setEventCallback(handleEvent); | 77 embedder.connectToService("mojo:camera", camera); |
| 95 getPhoto(); | 78 drawNextPhoto(); |
| 96 } | 79 } |
| OLD | NEW |