首页 > 代码库 > Android实战简易教程-第二十九枪(基于Face++实现年龄识别APP(一))

Android实战简易教程-第二十九枪(基于Face++实现年龄识别APP(一))

我们通过Face++提供的api和服务实现HowOld App的开发。首先我们实现图片的选择功能。

1.main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_getImage"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Get image"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp" />

    <Button
        android:id="@+id/btn_detect"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Detect"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/btn_getImage" />

    <TextView
        android:id="@+id/tv_tip"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="10dp"
        android:text="tip"
        android:paddingLeft="10dp"
        android:gravity="center"
        android:layout_alignTop="@+id/btn_detect"

        />

    <ImageView
        android:layout_above="@+id/btn_detect"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="http://www.mamicode.com/@drawable/t4"
        android:id="@+id/iv_photo" />

    <FrameLayout
        android:id="@+id/id_waitting"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:visibility="gone"
        android:clickable="true">

        <ProgressBar
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="center" />

    </FrameLayout>

</RelativeLayout>

2.MainActivity.java:

package com.yayun.howold;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity implements View.OnClickListener {
    private static final int PICK_CODE = 0x110;//随便设置一个常量
    private ImageView mPhoto;
    private Button mGetImage, mDetect;
    private TextView mTip;
    private View mWaitting;
    private String mCurrentPhotoStr;
    private Bitmap mPhotoImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();//初始化控件
        initEvents();//点击事件


    }

    private void initEvents() {
        mGetImage.setOnClickListener(this);
        mDetect.setOnClickListener(this);
    }

    private void initViews() {
        mPhoto = (ImageView) findViewById(R.id.iv_photo);
        mGetImage = (Button) findViewById(R.id.btn_getImage);
        mDetect = (Button) findViewById(R.id.btn_detect);
        mTip = (TextView) findViewById(R.id.tv_tip);
        mWaitting = (View) findViewById(R.id.id_waitting);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {//返回结果推断
        if (requestCode == PICK_CODE) {
            if (intent != null) {
                Uri uri = intent.getData();//获取uri
                //以下是为了获取图片的路径
                Cursor cursor = getContentResolver().query(uri, null, null, null, null);
                cursor.moveToFirst();

                int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);//获取索引
                mCurrentPhotoStr = cursor.getString(idx);//拿到图片的路径。当然我曾经的文章也有介绍怎么由uri获取string型的地址

                resizePhoto();//压缩图片
                mPhoto.setImageBitmap(mPhotoImg);//设置压缩后图片
                mTip.setText("Click Detect==>");

            }
        }
        super.onActivityResult(requestCode, resultCode, intent);
    }

    /**
     * 压缩图片
     */
    private void resizePhoto() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;//不会载入图片,仅仅会获取图片的宽高
        BitmapFactory.decodeFile(mCurrentPhotoStr, options);//options里已存取宽高

        double ratio = Math.max(options.outWidth * 1.0d / 1024f, options.outHeight * 1.0d / 1024f);//宽高尽可能小于1024,终于每一个图片不能超过3M
        options.inSampleSize = (int) Math.ceil(ratio);
        options.inJustDecodeBounds = false;//否则不会载入图片

        mPhotoImg = BitmapFactory.decodeFile(mCurrentPhotoStr, options);//压缩后的BitMap

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_getImage:
                Intent intent = new Intent(Intent.ACTION_PICK);//选择
                intent.setType("image/*");//图片格式
                startActivityForResult(intent, PICK_CODE);
                break;
            case R.id.btn_detect:
                break;

        }

    }
}
执行实例:

技术分享

选择图片:

技术分享

成功选择图片!

喜欢的朋友请关注我!

谢谢

Android实战简易教程-第二十九枪(基于Face++实现年龄识别APP(一))