To move it with keyboard shortcuts:
- Click to select Android Emulator window
- Press Alt + SPACE
- press m
- Up/Down/Left/Right to move the window.
package com.blogspot.android_er.androidsystemproperties;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Enumeration;
import java.util.Properties;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView info = (TextView)findViewById(R.id.info);
Properties properties = System.getProperties();
info.append(properties.toString());
info.append("\n----------\n\n");
Enumeration<String> prop =
(Enumeration<String>) properties.propertyNames();
while(prop.hasMoreElements()){
String propName = prop.nextElement();
info.append(propName + " : \n" +
System.getProperty(propName) + "\n\n");
}
}
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
On-Screen Keyboard run on Windows 10 |
Movie Maker run on Windows 10 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="@dimen/activity_horizontal_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/imageview"
android:layout_gravity="center"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:src="@mipmap/ic_launcher"
android:background="#cecece"/>
<TextView
android:id="@+id/textview1"
android:layout_below="@id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_marginStartPercent="25%"
app:layout_marginEndPercent="25%"
android:text="PercentRelativeLayout example"
android:background="#bebebe"/>
<TextView
android:id="@+id/textview2"
android:layout_below="@id/textview1"
android:layout_height="wrap_content"
app:layout_widthPercent="60%"
android:text="PercentRelativeLayout example"
android:background="#aeaeae"/>
</android.support.percent.PercentRelativeLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="@dimen/activity_horizontal_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<android.support.percent.PercentFrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/imageview"
android:layout_gravity="center"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:src="@mipmap/ic_launcher"
android:background="#cecece"/>
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_marginStartPercent="25%"
app:layout_marginEndPercent="25%"
android:text="PercentFrameLayout example"
android:background="#bebebe"/>
<TextView
android:id="@+id/textview2"
android:layout_height="wrap_content"
app:layout_widthPercent="60%"
android:text="PercentFrameLayout example"
android:background="#aeaeae"/>
</android.support.percent.PercentFrameLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
card_view:cardCornerRadius="20sp"
card_view:cardElevation="5sp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"/>
<TextView
android:id="@+id/item_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
package com.example.androidlistdrawable;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MyRecyclerViewAdapter extends
RecyclerView.Adapter<MyRecyclerViewAdapter.ItemHolder> {
private List<String> itemsName;
private List<Integer> itemsValue;
private LayoutInflater layoutInflater;
private Context context;
public MyRecyclerViewAdapter(Context context){
this.context = context;
layoutInflater = LayoutInflater.from(context);
itemsName = new ArrayList<String>();
itemsValue = new ArrayList<Integer>();
}
@Override
public MyRecyclerViewAdapter.ItemHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
CardView itemCardView = (CardView)layoutInflater.inflate(R.layout.layout_cardview, viewGroup, false);
return new ItemHolder(itemCardView, this);
}
@Override
public void onBindViewHolder(MyRecyclerViewAdapter.ItemHolder itemHolder, int i) {
itemHolder.setItemName(itemsName.get(i));
int value = itemsValue.get(i);
itemHolder.setItemValue(String.valueOf(value));
Drawable drawable = context.getResources().getDrawable(value);
itemHolder.setImageView(drawable);
}
@Override
public int getItemCount() {
return itemsName.size();
}
public void add(int location, String iName, int iValue){
itemsName.add(location, iName);
itemsValue.add(location, iValue);
notifyItemInserted(location);
}
public void remove(int location){
if(location >= itemsName.size())
return;
itemsName.remove(location);
itemsValue.remove(location);
notifyItemRemoved(location);
}
public static class ItemHolder extends RecyclerView.ViewHolder{
private MyRecyclerViewAdapter parent;
private CardView cardView;
TextView textItemName;
TextView textItemValue;
ImageView imageView;
public ItemHolder(CardView cView, MyRecyclerViewAdapter parent) {
super(cView);
cardView = cView;
this.parent = parent;
textItemName = (TextView) cardView.findViewById(R.id.item_name);
textItemValue = (TextView) cardView.findViewById(R.id.item_value);
imageView = (ImageView) cardView.findViewById(R.id.item_image);
}
public void setItemName(CharSequence name){
textItemName.setText(name);
}
public CharSequence getItemName(){
return textItemName.getText();
}
public void setItemValue(CharSequence val){
textItemValue.setText(val);
}
public CharSequence getItemValue(){
return textItemValue.getText();
}
public void setImageView(Drawable drawable){
imageView.setImageDrawable(drawable);
}
}
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="@+id/myrecyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
package com.example.androidlistdrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import java.lang.reflect.Field;
public class MainActivity extends AppCompatActivity {
private RecyclerView myRecyclerView;
private StaggeredGridLayoutManager staggeredGridLayoutManagerVertical;
private MyRecyclerViewAdapter myRecyclerViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myRecyclerView = (RecyclerView)findViewById(R.id.myrecyclerview);
staggeredGridLayoutManagerVertical =
new StaggeredGridLayoutManager(
2, //The number of Columns in the grid
LinearLayoutManager.VERTICAL);
myRecyclerViewAdapter = new MyRecyclerViewAdapter(this);
myRecyclerView.setAdapter(myRecyclerViewAdapter);
myRecyclerView.setLayoutManager(staggeredGridLayoutManagerVertical);
prepareItems();
}
private void prepareItems(){
Field[] fieldDrawables = android.R.drawable.class.getFields();
for(int i=0; i<fieldDrawables.length; i++){
Field field = fieldDrawables[i];
try {
int value = (int) field.get(fieldDrawables);
myRecyclerViewAdapter.add(
myRecyclerViewAdapter.getItemCount(),
"R.drawable." + field.getName(),
value);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
package com.example.androidlistdrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.lang.reflect.Field;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView info = (TextView)findViewById(R.id.info);
Field[] fieldDrawables = android.R.drawable.class.getFields();
for(int i=0; i<fieldDrawables.length; i++){
Field field = fieldDrawables[i];
info.append("R.drawable." + field.getName() + " :\n");
info.append(field.toString() + "\n");
try {
int value = (int) field.get(fieldDrawables);
info.append("value = " + value + "\n");
} catch (IllegalAccessException e) {
e.printStackTrace();
}
info.append("\n");
}
}
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
package com.example.androidfacedetection;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.face.Face;
import com.google.android.gms.vision.face.FaceDetector;
import com.google.android.gms.vision.face.Landmark;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final int RQS_LOADIMAGE = 1;
private Button btnLoad, btnDetFace;
private ImageView imgView;
private Bitmap myBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLoad = (Button)findViewById(R.id.btnLoad);
btnDetFace = (Button)findViewById(R.id.btnDetectFace);
imgView = (ImageView)findViewById(R.id.imgview);
btnLoad.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, RQS_LOADIMAGE);
}
});
btnDetFace.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(myBitmap == null){
Toast.makeText(MainActivity.this,
"myBitmap == null",
Toast.LENGTH_LONG).show();
}else{
detectFace();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RQS_LOADIMAGE
&& resultCode == RESULT_OK){
if(myBitmap != null){
myBitmap.recycle();
}
try {
InputStream inputStream =
getContentResolver().openInputStream(data.getData());
myBitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
imgView.setImageBitmap(myBitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/*
reference:
https://search-codelabs.appspot.com/codelabs/face-detection
*/
private void detectFace(){
//Create a Paint object for drawing with
Paint myRectPaint = new Paint();
myRectPaint.setStrokeWidth(5);
myRectPaint.setColor(Color.GREEN);
myRectPaint.setStyle(Paint.Style.STROKE);
Paint landmarksPaint = new Paint();
landmarksPaint.setStrokeWidth(10);
landmarksPaint.setColor(Color.RED);
landmarksPaint.setStyle(Paint.Style.STROKE);
Paint smilingPaint = new Paint();
smilingPaint.setStrokeWidth(4);
smilingPaint.setColor(Color.YELLOW);
smilingPaint.setStyle(Paint.Style.STROKE);
boolean somebodySmiling = false;
//Create a Canvas object for drawing on
Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);
tempCanvas.drawBitmap(myBitmap, 0, 0, null);
//Detect the Faces
//!!!
//Cannot resolve method setTrackingEnabled(boolean)
//FaceDetector faceDetector = new FaceDetector.Builder(getApplicationContext()).build();
//faceDetector.setTrackingEnabled(false);
FaceDetector faceDetector =
new FaceDetector.Builder(getApplicationContext())
.setTrackingEnabled(false)
.setLandmarkType(FaceDetector.ALL_LANDMARKS)
.setClassificationType(FaceDetector.ALL_CLASSIFICATIONS)
.build();
Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
SparseArray<Face> faces = faceDetector.detect(frame);
//Draw Rectangles on the Faces
for(int i=0; i<faces.size(); i++) {
Face thisFace = faces.valueAt(i);
float x1 = thisFace.getPosition().x;
float y1 = thisFace.getPosition().y;
float x2 = x1 + thisFace.getWidth();
float y2 = y1 + thisFace.getHeight();
tempCanvas.drawRoundRect(new RectF(x1, y1, x2, y2), 2, 2, myRectPaint);
//get Landmarks for the first face
List<Landmark> landmarks = thisFace.getLandmarks();
for(int l=0; l<landmarks.size(); l++){
PointF pos = landmarks.get(l).getPosition();
tempCanvas.drawPoint(pos.x, pos.y, landmarksPaint);
}
//check if this face is Smiling
final float smilingAcceptProbability = 0.5f;
float smilingProbability = thisFace.getIsSmilingProbability();
if(smilingProbability > smilingAcceptProbability){
tempCanvas.drawOval(new RectF(x1, y1, x2, y2), smilingPaint);
somebodySmiling = true;
}
}
imgView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));
if(somebodySmiling){
Toast.makeText(MainActivity.this,
"Done - somebody is Smiling",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,
"Done - nobody is Smiling",
Toast.LENGTH_LONG).show();
}
}
}
package com.example.androidfacedetection;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.face.Face;
import com.google.android.gms.vision.face.FaceDetector;
import com.google.android.gms.vision.face.Landmark;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final int RQS_LOADIMAGE = 1;
private Button btnLoad, btnDetFace;
private ImageView imgView;
private Bitmap myBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLoad = (Button)findViewById(R.id.btnLoad);
btnDetFace = (Button)findViewById(R.id.btnDetectFace);
imgView = (ImageView)findViewById(R.id.imgview);
btnLoad.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, RQS_LOADIMAGE);
}
});
btnDetFace.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(myBitmap == null){
Toast.makeText(MainActivity.this,
"myBitmap == null",
Toast.LENGTH_LONG).show();
}else{
detectFace();
Toast.makeText(MainActivity.this,
"Done",
Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RQS_LOADIMAGE
&& resultCode == RESULT_OK){
if(myBitmap != null){
myBitmap.recycle();
}
try {
InputStream inputStream =
getContentResolver().openInputStream(data.getData());
myBitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
imgView.setImageBitmap(myBitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/*
reference:
https://search-codelabs.appspot.com/codelabs/face-detection
*/
private void detectFace(){
//Create a Paint object for drawing with
Paint myRectPaint = new Paint();
myRectPaint.setStrokeWidth(5);
myRectPaint.setColor(Color.GREEN);
myRectPaint.setStyle(Paint.Style.STROKE);
Paint landmarksPaint = new Paint();
landmarksPaint.setStrokeWidth(10);
landmarksPaint.setColor(Color.RED);
landmarksPaint.setStyle(Paint.Style.STROKE);
//Create a Canvas object for drawing on
Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);
tempCanvas.drawBitmap(myBitmap, 0, 0, null);
//Detect the Faces
//!!!
//Cannot resolve method setTrackingEnabled(boolean)
//FaceDetector faceDetector = new FaceDetector.Builder(getApplicationContext()).build();
//faceDetector.setTrackingEnabled(false);
FaceDetector faceDetector =
new FaceDetector.Builder(getApplicationContext())
.setTrackingEnabled(false)
.setLandmarkType(FaceDetector.ALL_LANDMARKS)
.build();
Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
SparseArray<Face> faces = faceDetector.detect(frame);
//Draw Rectangles on the Faces
for(int i=0; i<faces.size(); i++) {
Face thisFace = faces.valueAt(i);
float x1 = thisFace.getPosition().x;
float y1 = thisFace.getPosition().y;
float x2 = x1 + thisFace.getWidth();
float y2 = y1 + thisFace.getHeight();
tempCanvas.drawRoundRect(new RectF(x1, y1, x2, y2), 2, 2, myRectPaint);
//get Landmarks for the first face
List<Landmark> landmarks = thisFace.getLandmarks();
for(int l=0; l<landmarks.size(); l++){
PointF pos = landmarks.get(l).getPosition();
tempCanvas.drawPoint(pos.x, pos.y, landmarksPaint);
}
}
imgView.setImageDrawable(new BitmapDrawable(getResources(),tempBitmap));
}
}
FaceDetector faceDetector = new FaceDetector.Builder(getApplicationContext()).build();
faceDetector.setTrackingEnabled(false);
FaceDetector faceDetector =
new FaceDetector.Builder(getApplicationContext())
.setTrackingEnabled(false)
.build();
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidfacedetection" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="face" />
</application>
</manifest>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18dp"
android:text="Face Detection with the Mobile Vision API" />
<Button
android:id="@+id/btnLoad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Photo"/>
<Button
android:id="@+id/btnDetectFace"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Detect Face" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgview"/>
</LinearLayout>
package com.example.androidfacedetection;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.face.Face;
import com.google.android.gms.vision.face.FaceDetector;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
private static final int RQS_LOADIMAGE = 1;
private Button btnLoad, btnDetFace;
private ImageView imgView;
private Bitmap myBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLoad = (Button)findViewById(R.id.btnLoad);
btnDetFace = (Button)findViewById(R.id.btnDetectFace);
imgView = (ImageView)findViewById(R.id.imgview);
btnLoad.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, RQS_LOADIMAGE);
}
});
btnDetFace.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(myBitmap == null){
Toast.makeText(MainActivity.this,
"myBitmap == null",
Toast.LENGTH_LONG).show();
}else{
detectFace();
Toast.makeText(MainActivity.this,
"Done",
Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RQS_LOADIMAGE
&& resultCode == RESULT_OK){
if(myBitmap != null){
myBitmap.recycle();
}
try {
InputStream inputStream =
getContentResolver().openInputStream(data.getData());
myBitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
imgView.setImageBitmap(myBitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/*
reference:
https://search-codelabs.appspot.com/codelabs/face-detection
*/
private void detectFace(){
//Create a Paint object for drawing with
Paint myRectPaint = new Paint();
myRectPaint.setStrokeWidth(5);
myRectPaint.setColor(Color.RED);
myRectPaint.setStyle(Paint.Style.STROKE);
//Create a Canvas object for drawing on
Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);
tempCanvas.drawBitmap(myBitmap, 0, 0, null);
//Detect the Faces
FaceDetector faceDetector = new FaceDetector.Builder(getApplicationContext()).build();
//!!!
//Cannot resolve method setTrackingEnabled(boolean)
//skip for now
//faceDetector.setTrackingEnabled(false);
Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
SparseArray<Face> faces = faceDetector.detect(frame);
//Draw Rectangles on the Faces
for(int i=0; i<faces.size(); i++) {
Face thisFace = faces.valueAt(i);
float x1 = thisFace.getPosition().x;
float y1 = thisFace.getPosition().y;
float x2 = x1 + thisFace.getWidth();
float y2 = y1 + thisFace.getHeight();
tempCanvas.drawRoundRect(new RectF(x1, y1, x2, y2), 2, 2, myRectPaint);
}
imgView.setImageDrawable(new BitmapDrawable(getResources(),tempBitmap));
}
}