Camera. Make new project – Give permission to use camera Make button called TakePicture Make button called ShowLastPic Add SurfaceView to layout – Surface.

Slides:



Advertisements
Similar presentations
- Rohit Vobbilisetty. What is the Camera API Capture Photos Capture Videos Camera API - Android2.
Advertisements

Threads, Surface Views and Real-Time Games. Background Most of the Android apps we’ve covered so far have been single threaded – And Event driven – An.
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Cosc 5/4730 Multimedia Part 1: Audio media. PLAYING AUDIO Android android.media.
Written by: Dr. JJ Shepherd
Multimedia.  Android provides built-in encoding/decoding for a variety of common media types  Allows you to play & record audio, still images & video.
Cosc 5/4730 Android media Part 2: Pictures and Video.
Recording and playing audio. App Make app AudioFun with 4 buttons – Start recording (id=StartRecording) – Stop recording (id=StopRecording) – Start playback.
Cannon Game 1 Fall 2014 CS7020: Game Design and Development.
App Inventor Barb Ericson July 3, 2013.
Camera. Make new project – Give permission to use camera Make button called TakePicture Make button called ShowLastPic Add SurfaceView to layout – Surface.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You’ll learn much less if you just look at the.
Video upload and streaming
Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access memory.
Android Development: File System Approaches Richard S. Stansbury 2015.
What to do when you are done. PRINTING WITH PHOTOSHOP.
Android Development (Basics)
Android Application Development Tutorial. Topics Lecture 5 Overview Overview of Networking Programming Tutorial 2: Downloading from the Internet.
Web server and web browser It’s a take and give policy in between client and server through HTTP(Hyper Text Transport Protocol) Server takes a request.
Cosc 5/4730 A little on threads and Messages: Handler class.
Android – Camera –an Example
CHAPTER 1 XNA Game Studio 4.0. Your First Project A computer game is not just a program—it is also lots of other bits and pieces that make playing the.
Java: Chapter 1 Computer Systems Computer Programming II Aug
Creating Mobile Apps with App Inventor! Day 5 Google search Kris Gordon Ludlow for links.
CS5103 Software Engineering Lecture 08 Android Development II.
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Content Providers & Services.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
MULTIMEDIA What is Multimedia? The word MULTIMEDIA is made up from two words, MULTI meaning more than one and MEDIA meaning a way of displaying or passing.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Java: Chapter 1 Computer Systems Computer Programming II.
Audio and Video CGS Some Common Audio Formats Format Use Extension MIDI instrumental music.mid MPEG songs.mp3 RealAudio live broadcasts.ra Wave.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Digital Camera Basics Misty Freeman Georgia CTAE Resource Network 2010.
Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal.
CSE 486/586, Spring 2012 CSE 486/586 Distributed Systems Recitation.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
20-Oct-15 Java 7. The diamond operator Instead of HashMap > map = new HashMap >(); you can now say HashMap > map = new HashMap; In fact, if you don’t.
Tracking Tasks and Processes. GET_TASK Make a new app, WatchProcesses – Add permission GET_TASK This permission allows the app to collect lots of information.
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Camera. Make new project – CameraFun – Give permission to use camera write_external _storage Make two buttons – id takePictureButton – id showLastPicButton.
Storing and Retrieving Data
ANDROID – DRAWING IMAGES – SIMPLE EXAMPLE IN INTERFACE AND EVENT HANDLING L. Grewe.
I. Getting Started with the Interface Microsoft ® Windows ® Movie Maker.
Import pictures I usually save them to my desktop Click here to browse files on your computer or images from the web.
CIS 3.5 Lecture 2.2 More programming with "Processing"
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Persistence Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
PROG Mobile Java Application Development PROG Mobile Java Application Development Data Storage, Continued.
Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using.
Camera. Make new project – Give permission to use camera Make button called TakePicture Make button called ShowLastPic Add SurfaceView to layout – Surface.
Android media Part 2: Pictures and Video
A UGMENTED F UTURE OF M OBILE R EALITY Oleg Novosad Mobile / Web / Game Engineer.
Photo Story. How to use Photo Story Photo Story 3 can be located in the Accessories folder on school computers. You will need to have your pictures already.
CS378 - Mobile Computing Audio.
Build Cycle 5.  Themes  US Presidents  Custom Images  Number of Moves Made  Posting to Facebook.
Android Application Audio 1.
Data Storage: Part 2 (File System)
CS371m - Mobile Computing Persistence.
Android Application Data Storage 1.
Camera.
CIS 470 Mobile App Development
null, true, and false are also reserved.
More programming with "Processing"
Units with – James tedder
Activities and Fragments
Presentation transcript:

camera

Make new project – Give permission to use camera Make button called TakePicture Make button called ShowLastPic Add SurfaceView to layout – Surface view is something we can draw on – This will show the picture preview – Set layout height = fill_parent (or try 10dip) – Set layout width = fill_parent – Set layout weight = 1 – Id SurfaceView changes when the phone’s orientation changes.

To handle the surface and changes in the surface, we need our activity to implement SurfaceHolder.Callback. – public class MyCamera extends Activity implements SurfaceHolder.Callback { Our MyCamera class needs some objects – Camera mCamera; // to hold the camera object – private SurfaceView mSurfaceView; // to hold the surface view – private SurfaceHolder mSurfaceHolder; // to hold the interface for the surfacevieww – We’ll add some more later After super.onCreate – setContentView(R.layout.main); – mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera); // get surface object – mSurfaceHolder = mSurfaceView.getHolder();// get surface holder – mSurfaceHolder.addCallback(this);// this activity is the callback – // note, instead this, we could make a new classss that implements SurfcaeHolder.Callback – mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // says ? Implementing SurfaceHolder.CallBack requires three functions to be implemented – surfaceCreated – surfaceChanged Called when orientation is changed and after surfaceCreated – surfaceDestroyed

public void surfaceCreated(SurfaceHolder holder) { Log.e("My camera app", "surfaceCreated"); mCamera = Camera.open();}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { Log.d("My camera app", "surfaceChanged"); But mCamera.stopPreview() requires that startPreview was called mCamera.stopPreview(); mCamera.startPreview(); …. Set up preview ….

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { Log.d(“My camera app”, "surfaceChanged"); If (previewRunning) mCamera.stopPreview(); mCamera.startPreview(); previewRunning = true; …. Set up preview …. boolean previewRunning = false;

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { Log.d(“My camera app”, "surfaceChanged"); if (previewRunning) { mCamera.stopPreview(); } Log.e("My camera app","view width"+w+" height"+h); Camera.Parameters p = mCamera.getParameters(); // set the preview size p.setPreviewSize(w, h); mCamera.setParameters(p); try { mCamera.setPreviewDisplay(holder); } catch (IOException e) { e.printStackTrace(); } mCamera.startPreview(); mPreviewRunning = true; }

public void surfaceDestroyed(SurfaceHolder holder) { Log.e("My camera app", "surfaceDestroyed"); mCamera.stopPreview(); mCamera.release(); previewRunning = false;}

run It crashes on nexus 1 Check log Note that error when setting preview size Note that preview size is odd. Check web for more info on Camera.Parameters and Camera.Parameters.setPreviewSize – see getSupportedPreviewSizes

Log.e("My camera app","width"+w+" h"+h); List sizes = mCamera.getParameters().getSupportedPreviewSizes(); Camera.Size sizeToSet = null; for (Camera.Size t: sizes) { String str = "view"; str += t.width; str += "by" + t.height; Log.e("My camera app”, str); } Log.e("My camera app“,"w="+sizeToSet.width+" h="+sizeToSet.height); Before setParameters, do the following if (t.width<=w && t.height<=h && found==false) { sizeToSet = t; found = true; } Boolean found = new Boolean(false); add Inside loop, add

Camera.Parameters p = mCamera.getParameters(); p.setPreviewSize(sizeToSet.width, sizeToSet.height); mCamera.setParameters(p); Change parameter setting to:

Rotation/orientation Note that the orientation is messed up – Perhaps the image is correct in only one orientation. Two fixes – Fix the orientation to be in portrait or landscape and set display orientation accordingly In onCreate, add – setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); In surfaceChanged, add – mCamera.setDisplayOrientation(180); // or whatever value works – Get the rotation and then set camera orientation accordingly In surfaceChanged, add – Display display = getWindowManager().getDefaultDisplay(); – switch (display.getRotation()) { » case Surface.ROTATION_0: Log.e("DEBUG INFO","rotation = 0"); mCamera.setDisplayOrientation(180); // whatever value works break; » case Surface.ROTATION_90: Etc. etc.

Take picture In onCreate add button click listener – When clicked call previewRunning = false; mCamera.stopPreview (); mCamera.takePicture(null, null, pictureCallback); – The first null could be set to play a sound. But we will just use the default – pictureCallback is a Camera.PictureCallback object that we must make

Where to save files? Private directories – Only accessible by this app (cannot share files) – Deleted when app is uninstalled – Can be on internal memory or external (e.g., sdcard) External Storage Directory – Might be on sdcard or internal memory – Can be mounted on another machine when attaching phone to laptop via usb, the external memory is accessible by the laptop and might not be accessible by the phone apps – No standard directories, so files can get messy. Hence not recommended unless you are careful to clean up after yourself External Storage Public Directory – Like External Storage Directory, but uses specific directories e.g., pictures, music, video

External storage Before external storage is used, the app must check that the storage is accessible – If the phone is attached to a computer via usb, the storage might not be accessible because the computer is controlling the storage device boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { – // We can read and write the media – mExternalStorageAvailable = mExternalStorageWriteable = true; – Log.e("DEBUG INFO","storage is readble and writable"); } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { – // We can only read the media – mExternalStorageAvailable = true; – mExternalStorageWriteable = false; – Log.e("DEBUG INFO","storage is readable only"); } else { – // Something else is wrong. It may be one of many other states, but all we need – // to know is we can neither read nor write – mExternalStorageAvailable = mExternalStorageWriteable = false; – Log.e("DEBUG INFO","storage is not accessible"); }

External Storage Public Directory If external storage is available, we can get the directory if (mExternalStorageWriteable) { – File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTO RY_PICTURES); – // arg must not be null, can be DIRECTORY_MUSIC, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, DIRECTORY_MOVIES, DIRECTORY_DOWNLOADS, or DIRECTORY_DCIM – Pcitures and videos from camera // this is a small set of directories – path.mkdirs(); – File file = new File(path, "my_pic.jpeg");

External Storage Directory if (mExternalStorageWriteable) { – File path = Environment. getExternalStorageDirectory(); – File file = new File(path, "my_pic.jpeg");

Application Storage Internal storage: only accessible by the applications. Security is enforced – File path = getFilesDir(); – File file = new File(path, "my_pic.jpeg"); External storage. Typically not visible to user media applications (e.g., if you store a movie here, your movie player will not find it) No security enforcement. So other applications could overwrite – File path = getExternalFilesDir (type); // type is DIRECTORY_MUSIC, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, or DIRECTORY_MOVIES. Or null – File file = new File(path, "my_pic.jpeg");

Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] imageData, Camera c) { if (imageData == null) { Log.e("DEBUG INFO","image is null"); } else { // get file as just explained We will use Bitmap, which has lots of bitmap related functions To write to a jpeg file, we use Bitmap.compress(file format, quality, outputStream); Bitmap bitmap = BitmapFactory.decodeByteArray(imageData,0,imageData.length); To create a Bitmap we use BitmapFactory. decodeByteArray(byte[] data, int offset, int length) Where data is imageData (the function argument,; offset = 0; and length = imageData.length FileOutputStream fos = new FileOutputStream(file); bitmap.compress(CompressFormat.JPEG, 50, fos); // quality is 50, but could be between 0 and 100 fos.close(); previewRunning = false; mCamera.startPreview(); Finally, start the preview again: add

Show picture Make new activity (see previous tutorial on making another activity) In layout add – Add ImageView In onCreate String pathForAppFiles = getFilesDir().getAbsolutePath(); pathForAppFiles = pathForAppFiles + "/" + "myPic.jpeg"; Log.d("Still image source filename:", pathForAppFiles); Get file name Get bitmap from file Bitmap bm = BitmapFactory.decodeFile(pathForAppFiles); Show bitmap in ImageView ImageView imageView = (ImageView)this.findViewById(R.id.ImageView); imageView.setImageBitmap(bm);