Kuo-Chin Lien
Kuo Chin Lien
`
Develop a Hello World project in Android
Studio
`
Capture, process, store, and display an image
on Android phones
on Android phones
` “If you have been using Eclipse with ADT, be aware
that Android Studio is now the official IDE for that Android Studio is now the official IDE for
Android, so you should migrate to Android Studio to receive all the latest IDE updates.”
` Where: ` Where:
http://developer.android.com/sdk/index.html ` System requirement (Windows):
◦ Microsoft® Windows® 8/7/Vista/2003 (32 or 64-bit) ◦ 2 GB RAM minimum, 4 GB RAM recommended
◦ 400 MB hard disk space + at least 1 GB for Android SDK, emulator system images, and caches
◦ 1280 x 800 minimum screen resolution ◦ Java Development Kit (JDK) 7
◦ Optional for accelerated emulator: Intel® processor with support for Intel® VT-x, Intel® EM64T (Intel® 64), and Execute Disable (XD) Bit functionality
`
AVD manager - choose a device for
emulation
◦ Tip: What’s the Android (Tools->Android->AVD Manager) ◦ Tip: What s the Android
version and API level of your phone?
`
SDK manager – download necessary
packages
packages
`
.java
C d h b h f
◦ Coding the behavior of your program
`
.xml
◦ Settings (GUI/hardware arrangement) for the appSettings (GUI/hardware arrangement) for the app `
.gradle
◦ Make file `
logcat
◦ In .java, use
Log.d(TAG, debug message); Log.d(TAG, debug_message);
.xml: GUI design
Logcat:
In .java, use
Log.d(TAG, debug_message);
to print debug_message at logcat at run time
` In the debug mode, you can pause at the
b k i i
breakpoints in your program
` To advance to the first line inside a method call,
click Step Into .
` To advance to the next line outside the current
method, click Step Out .
` To continue running the app normally ` To continue running the app normally,
2. Double click to edit the text
More More widgets
1.
Connect your Android phone to the
computer
2.
Turn on the phone, go to Developer Option,
enable USB D b
i
enable USB Debugging
`
A button to bring the user to the preview
mode which allows the user to take a picture.
`
The program automatically converts the
picture to grayscale
picture to grayscale
`
Display the grayscale image
`Save the grayscale image
`
Download:
http://opencv.org/
`Import OpenCV as a library
◦ www.ece.ucsb.edu/~kuochin/290I/SetupAS.html
`
Hardware permission
`GUI
`
Load OpenCV
d C llb k
`
BaseLoaderCallback
◦ Will be called after OpenCV is loaded
◦ Set up a listener here. When the button is clicked, Set up a ste e e e e t e butto s c c ed,
call captureImage.
`
captureImage
◦ Show the preview mode to the user ◦ Show the preview mode to the user ◦ Save an image
` Compose a camera intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name private static File getOutputMediaFile(int type) {
private static File getOutputMediaFile(int type) { … }
` Start the camera intent
startActivityForResult(intent CAPTURE IMAGE ACTIVITY REQUEST CODE); // will startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); // will enter the preview mode
` Receive the intent result
void onActivityResult (int requestCode, int resultCode, Intent data){ void onActivityResult (int requestCode, int resultCode, Intent data){ // process the captured frame here
}
` OpenCV provides various functions to process an
image: blur edge detection resize morphological image: blur, edge detection, resize, morphological operations…etc.
` We can call CvtColor( ) to transform an image from
RGB t th l
RGB to other color spaces:
Imgproc.cvtColor(InputBuffer, OutputBuffer, Imgproc.COLOR_RGB2GRAY); ` See more detail here:
htt //d / d l /i /d / i
http://docs.opencv.org/modules/imgproc/doc/mis cellaneous_transformations.html#cvtcolor
` See more functions here:
http://docs.opencv.org/modules/imgproc/doc/im gproc.html
` Sample code downloadable here:
h b d f b l h
http://cs.ucsb.edu/~efujimoto/mobile/Other/Main Activity.java
` Sample code for recording a video
◦ http://developer.android.com/training/camera/videobasics.htmlp p g
` Intent uses existing camera apps to help you capture images and
videos, which is convenient but less flexible. To provide more
compelling user experience (time lapse video, video snapshot …),
d k h l
p g p p p
you need to know the Camera class:
◦
http://developer.android.com/guide/topics/media/camera.html#custom-camera
◦ http://developer.android.com/reference/android/hardware/Camera.html
F API > 21 C 2
` For API >= 21, you can use Camera2
◦ http://developer.android.com/reference/android/hardware/camera2/pack
age-summary.html
◦ https://developer.android.com/samples/Camera2Basic/index.html
` OpenCV
◦ OpenCV tutorial: http://docs.opencv.org/doc/tutorials/tutorials.html
` To accomplish the homework, you need the permissions to ACCESS_FINE_LOCATION and/or others … ACCESS_COARSE_LOCATION INTERNET . .. . S li f h i i
` See a list of the permissions you can request:
http://developer.android.com/reference/android/Manifest.pe rmission.html
` Location: A data class representing a geographic location.
` LocationManager: A class provides access to the system location ` LocationManager: A class provides access to the system location
services
private LocationManager locationManager;
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider); .
. .
` See the following links for details and more sample codes: ` See the following links for details and more sample codes:
◦ http://developer.android.com/reference/android/location/Locatio
n.html
◦ http://developer.android.com/reference/android/location/Locatio
private SensorManager sensorManager; private Sensor gyro;
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); gyro = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
sensorManager.registerListener(this, gyro, SensorManager.SENSOR_DELAY_NORMAL);
. . .
public void onSensorChanged(SensorEvent event) { // read the sensor output and do something … .
. }
` See the following links for more sensors with sample codes
◦ http://developer.android.com/reference/android/hardware/Senso r html r.html ◦ http://developer.android.com/reference/android/hardware/Senso rManager.html ◦ http://developer.android.com/reference/android/hardware/Senso rEvent.html rEvent.html
`
Find this presentation at
http://www.ece.ucsb.edu/~kuochin/290I/29
0I_Android_Tutorial.pdf
`