Camera. Make new project – CameraFun – Give permission to use camera write_external _storage Make two buttons – id takePictureButton – id showLastPicButton.

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial. Topics Lecture 4 Overview Overview of Sensors Programming Tutorial 1: Tracking location with GPS and Google.
Advertisements

- Rohit Vobbilisetty. What is the Camera API Capture Photos Capture Videos Camera API - Android2.
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.
Grade 6 Hopewell Elementary You will read each slide, then try to think of the answer. When you think you know the answer, click ONCE on the mouse.
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.
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.
Camera. Make new project – Give permission to use camera Make button called TakePicture Make button called ShowLastPic Add SurfaceView to layout – Surface.
1 From Yesterday private = accessible only to the class that declares it public = accessible to any class at all protected = accessible to the class and.
® Microsoft Office 2010 PowerPoint Tutorial 2: Adding and Modifying Text and Graphic Objects.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Video upload and streaming
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.
Multimedia.
Android – Camera –an Example
Java: Chapter 1 Computer Systems Computer Programming II Aug
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
CS5103 Software Engineering Lecture 08 Android Development II.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Java: Chapter 1 Computer Systems Computer Programming II.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
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.
Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal.
Applets Life Cycle Drawing and Event Handling Methods for UI Components Applet Capabilities Example.
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
The Java Programming Language
Photo Story 3 Importing Pictures. When you create a new Photo Story, the first thing that you will do is import pictures. You can import pictures from.
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.
1 Enter rotation angle Click Rotate for rotate and flip options.
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.
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
Cosc 4730 Android Fragments. Fragments You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own.
Greenfoot Game Programming Club.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Mobile Device Development Camera and Sensors Dr.YingLiang Ma.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
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.
CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
8.実機で実行 8-1 カメラ・録音関係 1.写真の撮影 A. マニフェストの設定 【 AndroidManifest.xml 】 ・・・ "
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
By: Eliav Menachi.  Android custom 2D graphics library  OpenGL ES 1.0 for high performance 3D graphics.
POWERPOINT : GETTING STARTED WITH POWERPOINT 1.Ribbon: customize ribbon – right click on ribbon and customize ribbon opens 2.Quick access toolbar.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,
Mobile Software Development for Android - I397 IT COLLEGE, ANDRES KÄVER, WEB:
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
Multimedia.
Camera.
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Manipulating Pictures, Arrays, and Loops part 2
null, true, and false are also reserved.
An overview of Java, Data types and variables
Chapter 1: Computer Systems
Cannon Game App Android How to Program
JavaScript Reserved Words
Focus of the Course Object-Oriented Software Development
Activities and Fragments
Presentation transcript:

camera

Make new project – CameraFun – Give permission to use camera write_external _storage Make two buttons – id takePictureButton – id showLastPicButton Add SurfaceView to layout – Surface view is something we can draw on Graphics on a SurfaceView is discussed in the video GraphicWithCanvas – 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.

SurfaceView To handle the surface and changes in the surface, we need our activity to implement SurfaceHolder.Callback. – public class CameraFunActivity extends Activity implements SurfaceHolder.Callback { – See Graphics with Canvas notes for how to make surfaceView a class, not the activity Our CameraFunActivity class needs some objects – Camera mCamera; // to hold the camera object – //!! When importing package camera, make sure to get android.hardware.camera – private SurfaceView mSurfaceView; // to hold the surface view – private SurfaceHolder mSurfaceHolder; // to hold the interface for the surfaceview – boolean previewRunning = false; – We’ll add some more later In onCreate, add – 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 class that implements SurfaceHolder.Callback – mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); Implementing SurfaceHolder.CallBack requires three functions to be implemented – surfaceCreated – surfaceChanged Called when orientation is changed and after surfaceCreated – surfaceDestroyed

surfaceCreated and surfaceDestroyed public void surfaceCreated(SurfaceHolder holder) { – Log.e("CameraFun", "surfaceCreated"); – mCamera = Camera.open(); // this will not compile if the wrong camera package was imported (see previous slide) – } public void surfaceDestroyed(SurfaceHolder holder) { Log.e("CamerFun", "surfaceDestroyed"); mCamera.stopPreview(); mCamera.release(); previewRunning = false; }

surfaceChanged public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { – // called when surface created and when orientation is changed – Log.d("CameraFun", "surfaceChanged"); – if (previewRunning) mCamera.stopPreview(); – Log.e("CameraFun","view width"+width+" height"+height); – Camera.Parameters p = mCamera.getParameters(); // set the preview size – p.setPreviewSize(width, height); – mCamera.setParameters(p); – try { mCamera.setPreviewDisplay(holder); – } catch (IOException e) { e.printStackTrace(); – } – mCamera.startPreview(); – mPreviewRunning = true; }

run It crashes on most devices 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

Improved surfaceChanged In surfaceChanged, before setParameters, add – Log.e("MyCameraFun","width"+width+" h"+height); – 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("CameraFun", str); – if (t.width<= width && t.height<= height && sizeToSet == null) – sizeToSet = t; – } – Log.e("CameraFun","w="+sizeToSet.width+" h="+sizeToSet.height); change – p.setPreviewSize(width, height); – to – p.setPreviewSize(sizeToSet.width, sizeToSet.height); – // note that this sizetoSet might be null. In which case none of the supported sizes works. This condition should be handled run

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 – In version 2.2 and above: » mCamera.setDisplayOrientation(180); // or 90 or whatever value works – In 2.1 » Camera.Parameters parameters = mCamera.getParameters(); parameters.set("orientation", "portrait"); mCamera.setParameters(parameters); – 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 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

PictureCallback Camera.PictureCallback pictureCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] imageData, Camera c) { – if (imageData == null) { Log.e("DEBUG INFO","image is null"); – } else { File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); File fileToSave = new File(path,"myPic.jpeg"); Log.e("CameraFun","saving pic to: "+fileToSave); Bitmap bitmap = BitmapFactory.decodeByteArray(imageData,0,imageData.length); FileOutputStream fos = new FileOutputStream(fileToSave); bitmap.compress(CompressFormat.JPEG, 50, fos); // quality is 50, but could be between 0 and 100 fos.close(); } – } – mCamera.startPreview(); }; Notes: there are many bitmap functions. Check documentation Need to something to handle throw Run Get pic from storage and view on your laptop

Show picture Make new activity (see previous tutorial on making another activity) In layout add – Add ImageView In onCreate, add – // get file with picture – File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); – File fileToView = new File(path,"myPic.jpeg"); – Log.e("Still image source filename:", fileToView.getPath()); – // load file as bitmap – Bitmap bm = BitmapFactory.decodeFile(fileToView.getPath()); – // show bitmap – android.widget.ImageView imageView = (android.widget.ImageView)findViewById(R.id.imageView); – imageView.setImageBitmap(bm);

Record video Add android.permission.RECORD_AUDIO Add member variable: MediaRecorder mediaRecorder; Add button for stopping the record – We’ll use the button for taking a picture to start recording In the start recording button, replace – mCamera.takePicture(null, null, pictureCallback); With – File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES); – File fileToSave = new File(path,"myMov.mpeg"); – mCamera.unlock(); – mediaRecorder = new MediaRecorder(); – mediaRecorder.setCamera(mCamera); – mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); – mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); – mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); – mediaRecorder.setMaxDuration(-1); – mediaRecorder.setOutputFile(fileToSave.getPath()); // your file – mediaRecorder.setVideoFrameRate(10);//videoFramesPerSecond); – mediaRecorder.setVideoSize(320, 240); // or some other size – mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); – mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); – mediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); – mediaRecorder.setMaxFileSize( ); // size in bytes – mediaRecorder.prepare(); – mediaRecorder.start();

In stop video button mediaRecorder.stop(); mCamera.lock();

Playing Video in picviewer.xml (layout), – delete image view – Add VideoView in ImageView – Comment out everything with showing the picture – Add File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES); File fileToLoad = new File(path,"myMov.mpeg"); VideoView mVideoView = (VideoView) findViewById(R.id.videoView); mVideoView.setVideoPath(fileToLoad.getPath()); mVideoView.setMediaController(new MediaController(this)); mVideoView.requestFocus(); mVideoView.start(); //mVideoView.stop() //mVideoView.suspend();