Mobile Device Development Camera and Sensors Dr.YingLiang Ma.

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

Cosc 5/4730 Android media Part 2: Pictures and Video.
Sensors.  Hardware devices that take measurements of the physical environment  Some examples  3-axis Accelerometer  3-axis Magnetic field sensor 
Android Application Development with Java UPenn CS4HS 2011 Chris Murphy
CS378 - Mobile Computing Sensing and Sensors. Sensors "I should have paid more attention in Physics 41" Most devices have built in sensors to measure.
CS378 - Mobile Computing Sensing and Sensors. Sensors "I should have paid more attention in Physics 41" Most devices have built in sensors to measure.
Android Application Development Tutorial. Topics Lecture 5 Overview Overview of Networking Programming Tutorial 2: Downloading from the Internet.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
Android Sensors & Async Callbacks Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
Introduction to Android Programming Content Basic environmental structure Building a simple app Debugging.
Android development the first app. Andoid vs iOS which is better? Short answer: neither Proponents on both sides For an iOS side, see this article on.
Programming Mobile Applications with Android September, Albacete, Spain Jesus Martínez-Gómez.
Getting Started with Android APIs Ivan Wong. Motivation - “Datasheet” - Recently exposed to what’s available in Android - So let’s see what API’s are.
Mobile Application Development Selected Topics – CPIT 490
Sensing. Possible sensors on devices – Documented in SensorEvent class Accelerometer (m/s 2 ) – acceleration in x, y, z axes Magnetic Field (micro Tesla)
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
1/29/ Android Programming: FrameLayout By Dr. Ramji M. Makwana Professor and Head, Computer Engineering Department A.D. Patel.
Android - Broadcast Receivers
Sensors – Part I SE 395/595.
로봇을 조종하자 3/4 UNIT 17 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 스마트 폰의 센서를 사용할 수 있다. 2.
CS378 - Mobile Computing Sensing and Sensors Part 2.
Import import android.graphics.Bitmap; import android.widget.ImageView;
Camera. Make new project – CameraFun – Give permission to use camera write_external _storage Make two buttons – id takePictureButton – id showLastPicButton.
Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.
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.
Sensors Nasrullah Khan Niazi. Using Device Sensors The Android SDK provides access to raw data from sensors on the device.The sensors,and their precision.
Sensors For Mobile Phones  Ambient Light Sensor  Proximity Sensor  GPS Receiver Sensor  Gyroscope Sensor  Barometer Sensor  Accelerometer Sensor.
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors Date: Feb 11, 2016.
CPE 490/590 – Smartphone Development
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
CHAPTER 8 Sensors and Camera. Chapter objectives: Understand Motion Sensors, Environmental Sensors and Positional Sensors Learn how to acquire measurement.
Mobile Software Development for Android - I397 IT COLLEGE, ANDRES KÄVER, WEB:
CS371m - Mobile Computing Sensing and Sensors.
The Doodlz app enables you to paint by dragging one or more fingers across the screen. The app provides options for setting the drawing color.
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Android Android Sensors Android Sensors: – Accelerometer – Gravity sensor – Linear Acceleration sensor – Magnetic Field sensor – Orientation.
Android Programming.
Sensors in Android.
Vijay Kumar Kolagani Dr. Yingcai Xiao
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors
Lab7 – Appendix.
Introduction to android
GUI Programming Fundamentals
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors.
Android Widgets 1 7 August 2018
Vijay Kumar Kolagani Dr. Yingcai Xiao
Android – Read/Write to External Storage
CS499 – Mobile Application Development
Android Introduction Camera.
Mobile Device Development
CS371m - Mobile Computing Sensing and Sensors.
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CS323 Android Model-View-Controller Architecture
Vijay Kumar Kolagani Dr. Yingcai Xiao
CIS 470 Mobile App Development
Android Sensor Programming
Many thanks to Jun Bum Lim for his help with this tutorial.
Activities and Intents
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors.
Mobile Programming Sensors in Android.
Mobile Programming Gestures in Android.
Adding Components to Activity
BLP 4216 MOBİL UYGULAMA GELİŞTİRME-2
CMPE419 Mobile Application Development
Mobile Programming Broadcast Receivers.
Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

Mobile Device Development Camera and Sensors Dr.YingLiang Ma

UFCFX5-15-3Mobile Device Development Agenda Android Camera Overview Simple android app using camera Using Camera API Using

UFCFX5-15-3Mobile Device Development Camera in android mobile

UFCFX5-15-3Mobile Device Development Camera Specifications 1.Camera resolutions (image sensors) 2.Optical Zoom or Digital Zoom 3.Xenon flash or LED flash 4.Video capture resolutions (4K??) 5.Face recognition 6. Low-light performance 7.Anti-shake performance

UFCFX5-15-3Mobile Device Development Camera Quality variation 1.Camera resolutions 2.With or without flash 3.No face-forward camera 4.No advance features

UFCFX5-15-3Mobile Device Development Simple Camera App Using existing android camera application in our application Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 1ACTION_IMAGE_CAPTURE_SECURE It returns the image captured from the camera, when the device is secured 2ACTION_VIDEO_CAPTURE It calls the existing video application in android to capture video 3EXTRA_SCREEN_ORIENTATION It is used to set the orientation of the screen to vertical or landscape 4EXTRA_FULL_SCREEN It is used to control the user interface of the ViewImage

UFCFX5-15-3Mobile Device Development UI Design <RelativeLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:textAppearance="?android:attr/textAppearanceLarge" />

UFCFX5-15-3Mobile Device Development String definition Camera Settings Hello world! Tap the image to open the camera!!

UFCFX5-15-3Mobile Device Development Main Activity public class MainActivity extends Activity { ImageView protected void onCreate(Bundle savedInstanceState) { imgFavorite.setOnClickListener(new OnClickListener() public void onClick(View v) {open(); } }); } public void open(){ Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 0); protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); Bitmap bp = (Bitmap) data.getExtras().get("data"); imgFavorite.setImageBitmap(bp); }

UFCFX5-15-3Mobile Device Development Result

UFCFX5-15-3Mobile Device Development Camera API app We will be using the camera API to integrate the camera in our application Class Description Camera It is used to control the camera and take images or capture video from the camera SurfaceView This class is used to present a live camera preview to the user.

UFCFX5-15-3Mobile Device Development UI Design <LinearLayout xmlns:android=" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="0.30" android:orientation="vertical" > <FrameLayout android:layout_width="fill_parent" android:layout_height="400dp" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="snapIt" /> <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitXY" />

UFCFX5-15-3Mobile Device Development UI

UFCFX5-15-3Mobile Device Development Android Manifest file <manifest xmlns:android=" package="com.example.camera1" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application > <activity android:name="com.example.camera1.MainActivity" android:configChanges="orientation" android:screenOrientation="portrait">

UFCFX5-15-3Mobile Device Development Structure MainActivity ShowCamera Preview windows Main UI FrameLayout AddView PictureCallback Take photo

UFCFX5-15-3Mobile Device Development Show Camera Activity public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder holdMe; private Camera theCamera; public ShowCamera(Context context,Camera camera) { super(context); theCamera = camera; holdMe = getHolder(); holdMe.addCallback(this); public void surfaceCreated(SurfaceHolder holder) { try { theCamera.setPreviewDisplay(holder); theCamera.startPreview(); } catch (IOException e) { public void surfaceDestroyed(SurfaceHolder arg0) { }

UFCFX5-15-3Mobile Device Development Main Activity public class MainActivity extends Activity { private Camera cameraObject; private ShowCamera showCamera; private ImageView protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pic = (ImageView)findViewById(R.id.imageView1); cameraObject = isCameraAvailiable(); showCamera = new ShowCamera(this, cameraObject); FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); preview.addView(showCamera); }

UFCFX5-15-3Mobile Device Development Camera thread private PictureCallback capturedIt = new PictureCallback() public void onPictureTaken(byte[] data, Camera camera) { Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); if(bitmap==null){ Toast.makeText(getApplicationContext(), "not taken", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "taken", Toast.LENGTH_SHORT).show(); pic.setImageBitmap(bitmap); } cameraObject.startPreview(); } };

UFCFX5-15-3Mobile Device Development Save the picture Put SD card write permission in android Manifest file In PictureCallback function Toast.makeText(getApplicationContext(), "taken", Toast.LENGTH_SHORT).show(); pic.setImageBitmap(bitmap); String path = Environment.getExternalStorageDirectory().toString(); String filename = path + "/SpyApp"+String.format("%d.jpg", System.currentTimeMillis()); FileOutputStream outStream = null; try { outStream = new FileOutputStream(filename); outStream.write(data); outStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { }

UFCFX5-15-3Mobile Device Development See the picture files Download File Manage from Google Play Store

UFCFX5-15-3Mobile Device Development See the picture files

UFCFX5-15-3Mobile Device Development See the picture files

UFCFX5-15-3Mobile Device Development Record audio If you want to have more control over audio recording you could use the MediaRecorder.

UFCFX5-15-3Mobile Device Development Example codes Set audio permission in android Manifest file MediaRecorder recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(PATH_NAME); recorder.prepare(); recorder.start(); // Recording is now started... recorder.stop(); recorder.reset(); // You can reuse the object by going back to setAudioSource() step recorder.release(); // Now the object cannot be reused

UFCFX5-15-3Mobile Device Development Add them to MainActivity Set audio permission in android Manifest file protected void onCreate(Bundle savedInstanceState) { ….. ///record audio String path = Environment.getExternalStorageDirectory().toString(); String filename = path + "/SpyAudio"+String.format("%d.3gp", System.currentTimeMillis()); recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(filename); try { recorder.prepare(); } catch (IllegalStateException e) {e.printStackTrace();} recorder.start(); // Recording is now started

UFCFX5-15-3Mobile Device Development Stop public boolean onKeyDown(int keyCode, KeyEvent event) { //Handle the back button if(keyCode == KeyEvent.KEYCODE_BACK) { recorder.stop(); recorder.reset(); // You can reuse the object by going back to setAudioSource() step recorder.release(); // Now the object cannot be reused return true; } else return super.onKeyDown(keyCode, event); }

UFCFX5-15-3Mobile Device Development Use Sensor in Android App Motion sensors These sensors measure acceleration forces and rotational forces along three axes. This category includes accelerometers, gravity sensors, gyroscopes, and rotational vector sensors. Environmental sensors These sensors measure various environmental parameters, such as ambient air temperature and pressure, illumination, and humidity. This category includes barometers, photometers, and thermometers. Position sensors These sensors measure the physical position of a device. This category includes orientation sensors and magnetometers.

UFCFX5-15-3Mobile Device Development Use Sensor in Android App You can access sensors available on the device and acquire raw sensor data by using the Android sensor framework. Determine which sensors are available on a device. Determine an individual sensor's capabilities, such as its maximum range, manufacturer, power requirements, and resolution. Acquire raw sensor data and define the minimum rate at which you acquire sensor data. Register and unregister sensor event listeners that monitor sensor changes.

UFCFX5-15-3Mobile Device Development Sensor availablity Few Android-powered devices have every type of sensor. For example, most handset devices and tablets have an accelerometer and a magnetometer, but fewer devices have barometers or thermometers. Also, a device can have more than one sensor of a given type. For example, a device can have two gravity sensors, each one having a different range. Identifying Sensors and Sensor Capabilities private SensorManager mSensorManager; mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); List deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);

UFCFX5-15-3Mobile Device Development Monitoring Sensor Events public class SensorActivity extends Activity implements SensorEventListener { private SensorManager mSensorManager; private Sensor public final void onSensorChanged(SensorEvent event) { // The light sensor returns a single value. // Many sensors return 3 values, one for each axis. float lux = event.values[0]; // Do something with this sensor value. }

UFCFX5-15-3Mobile Device Development Handling Different Sensor Configurations private SensorManager mSensorManager;... mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); if (mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE) != null){ // Success! There's a pressure sensor. } else { // Failure! No pressure sensor. } Using Google Play filters to target specific sensor configurations <uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />

UFCFX5-15-3Mobile Device Development Best Practices for Accessing and Using Sensors Unregister sensor listeners Don't test your code on the emulator Don't block the onSensorChanged() method (do as little as possible within the onSensorChanged(SensorEvent) method as sensor data can change at a high rate) Avoid using deprecated methods or sensor types Verify sensors before you use them Choose sensor delays carefully

UFCFX5-15-3Mobile Device Development Using the Proximity Sensor private SensorManager mSensorManager; private Sensor mSensor;... mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mSensor = public final void onSensorChanged(SensorEvent event) { float distance = event.values[0]; // Do something with this sensor data. }

UFCFX5-15-3Mobile Device Development Using the Orientation Sensor The orientation sensor lets you monitor the position of a device relative to the earth's frame of reference (specifically, magnetic north). private SensorManager mSensorManager; private Sensor mSensor;... mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mSensor = public void onSensorChanged(SensorEvent event) { float azimuth_angle = event.values[0]; float pitch_angle = event.values[1]; float roll_angle = event.values[2]; // Do something with these orientation angles. }

UFCFX5-15-3Mobile Device Development Using the Step Counter Sensor The step counter sensor provides the number of steps taken by the user since the last reboot while the sensor was activated. The step counter has more latency (up to 10 seconds) but more accuracy than the step detector sensor. private SensorManager mSensorManager; private Sensor mSensor;... mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);

UFCFX5-15-3Mobile Device Development Environment Sensors Unlike most motion sensors and position sensors, which return a multi-dimensional array of sensor values for each SensorEvent, environment sensors return a single sensor value for each data event. TYPE_AMBIENT_TEMPERATURE event.values[0]°C Ambient air temperature. TYPE_LIGHT event.values[0] lx Illuminance. TYPE_PRESSURE event.values[0 hPa or mbar Ambient air pressure. TYPE_RELATIVE_HUMIDITYevent.values[0]% Ambient relative humidity. TYPE_TEMPERATUREevent.values[0]°C Device temperature.