{"id":161,"date":"2014-10-16T02:52:19","date_gmt":"2014-10-16T09:52:19","guid":{"rendered":"http:\/\/appinventor.pevest.com\/?p=161"},"modified":"2014-10-16T02:52:19","modified_gmt":"2014-10-16T09:52:19","slug":"using-images-and-the-camera-in-app-inventor","status":"publish","type":"post","link":"https:\/\/coldstreams.com\/appinventor\/2014\/10\/16\/using-images-and-the-camera-in-app-inventor\/","title":{"rendered":"Using Images and the Camera in App Inventor"},"content":{"rendered":"<p>In an earlier post, I mentioned that <em>ImageSprites<\/em> use images &#8211; and suggested I do a tutorial on images so here is a first look at using images in App Inventor 2.<\/p>\n<p>For this tutorial, we create a very simple app that uses the camera to take a picture, or we can fetch a selected photo from the\u00a0phone&#8217;s photo gallery. Either way, the image that is obtained is displayed on the screen.<\/p>\n<p>First, create a simple user interface. To set this up, I used the <em>VerticalArrangement<\/em> layout and then added a button, an <em>ImagePicker<\/em> and an <em>Image<\/em> on to the Viewer:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Image1-UserInterface.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-153 size-full\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Image1-UserInterface.png\" alt=\"Image1-UserInterface\" width=\"300\" height=\"491\" \/><\/a><\/p>\n<p>The <em>Image<\/em> component is located in the User Interface section of the Palette, in the Designer.<\/p>\n<p>The <em>ImagePicker<\/em> is located in the <em>Media<\/em> section of the Palette. Drag each to the Viewer window.<\/p>\n<p>You also need to drag a <em>Camera<\/em> component to the Viewer; the <em>Camera<\/em> is also located in the Media section. Note that the <em>Camera<\/em> is an invisible component and will appear below your app&#8217;s user interface screen, as shown above.<\/p>\n<p>After renaming the components, we should have a set of components like this:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Image1-Components.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-154\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Image1-Components.png\" alt=\"Image1-Components\" width=\"196\" height=\"182\" \/><\/a><\/p>\n<p>For the <em>Image1<\/em> component, the Properties look like:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Image1-Properties1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-155\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Image1-Properties1.png\" alt=\"Image1-Properties1\" width=\"212\" height=\"296\" \/><\/a><\/p>\n<p>Note that the text box field named <em>Picture<\/em> has the name of a .jpg image file entered into, in this example. Sorry for the ugly gibberish filename &#8211; I just grabbed a photo off of <a href=\"https:\/\/www.flickr.com\/photos\/edwardmitchell\">my own Flickr page<\/a>, saved to my hard drive and then uploaded to App Inventor.<\/p>\n<p>To upload a photo from your computer to the <em>Image1<\/em> control, click on the <em>Picture<\/em> field &#8211; this displays a prompt:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Image1-Properties2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-156 size-medium\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Image1-Properties2-224x300.png\" alt=\"Image1-Properties2\" width=\"224\" height=\"300\" \/><\/a><\/p>\n<p>The prompt shows the full name of the image I have already uploaded, and this file is already highlighted. Or, click on <em>Upload File &#8230;<\/em> at bottom to select and upload your own .jpg image file.<\/p>\n<p>This uploaded .jpg becomes the default image initially displayed in the program. If you want, you could leave this blank but having something to show on screen\u00a0makes the program a bit more interesting and helps the user to see where photos will be displayed.<\/p>\n<p><strong>The Blocks<\/strong><\/p>\n<p>Taking a photo is absurdly easy. When the button for taking a photo (<em>btnTakePhoto<\/em>) is clicked on (or rather, pressed with your finger), the <em>Click<\/em> event is raised &#8211; and all this event handled need do is call the camera&#8217;s <em>TakePicture<\/em> method:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Image1-TakingPhoto.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-157\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Image1-TakingPhoto-300x164.png\" alt=\"Image1-TakingPhoto\" width=\"300\" height=\"164\" \/><\/a><\/p>\n<p>Once the camera completes taking the picture, the <em>AfterPicture<\/em> event occurs. This is where we fetch the image (hover your mouse over\u00a0the pink &#8220;<em>image<\/em>&#8221; within the <em>AfterPicture<\/em> block and select &#8220;get image&#8221;). Assign the image that was just taken (the get image) to the Picture property of the <em>Image1<\/em> control. This copies the photo that was just taken, and displays it on screen.<\/p>\n<p>We could stop here, but let&#8217;s take a look at grabbing a picture from the photo gallery on the phone. To implement this, we use\u00a0<em>ImagePicker<\/em>. The <em>ImagePicker<\/em> is a control that displays the photo gallery, let&#8217;s you select\u00a0an image from the photo gallery, and then returns that selected image to the app.<\/p>\n<p>The <em>ImagePicker<\/em> displayed in the Designer is effectively a button that contains its own event handler for the <em>Click<\/em> event. Pressing the <em>ImagePicker<\/em> button on the main screen automatically enters the gallery where you select your photo. When the selection is complete, the <em>ImagePicker&#8217;s<\/em> <em>AfterPicking<\/em> event is raised and we add a tiny bit of code to process the image, merely copying the &#8220;<em>Selection<\/em>&#8221; property of the <em>ImagePicker<\/em> control (renamed here as <em>impgSelectPhoto<\/em>) to the <em>Image<\/em> control on screen to display the selected photo.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Image1-ImagePickerAfter.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-158\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Image1-ImagePickerAfter-300x50.png\" alt=\"Image1-ImagePickerAfter\" width=\"402\" height=\"67\" \/><\/a><\/p>\n<p><strong>Running the App<\/strong><\/p>\n<p>When activating the camera, take a photo and wait a moment &#8211; you should see a &#8220;checkmark&#8221; at the bottom of the screen after the photo is processed. \u00a0Tap the checkmark icon to signify you are done and you should be returned to the\u00a0app and the photo you took should be displayed.<\/p>\n<p>When selecting a photo from the gallery, wait for the gallery to appear, scroll through and select a photo. Tap the photo and wait for a moment &#8211; and control should return to the app and the selected photo will appear on the screen.<\/p>\n<p>Note &#8211; if you have more than one photo gallery app registered on your phone (I have 3 such apps), Android will prompt you as to which of the gallery apps you wish to use.<\/p>\n<p>There are additional features that are not shown here. For example, if your phone has a front facing and back facing camera, its possible to switch between the cameras.<\/p>\n<p><strong>SOURCE CODE<\/strong><\/p>\n<p>Source code is available in the MIT App Inventor Gallery &#8211;&gt; <a href=\"http:\/\/ai2.appinventor.mit.edu\/?locale=en#6233507322658816\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In an earlier post, I mentioned that ImageSprites use images &#8211; and suggested I do a tutorial on images so here is a first look at using images in App Inventor 2. For this tutorial, we create a very simple app that uses the camera to take a picture, or we can fetch a selected &hellip; <a href=\"https:\/\/coldstreams.com\/appinventor\/2014\/10\/16\/using-images-and-the-camera-in-app-inventor\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using Images and the Camera in App Inventor<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-161","post","type-post","status-publish","format-standard","hentry","category-components"],"_links":{"self":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/comments?post=161"}],"version-history":[{"count":0,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/161\/revisions"}],"wp:attachment":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/media?parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/categories?post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/tags?post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}