Face recognition online music, an immature Android APP developed by myself

Face recognition online music, an immature Android APP developed by myself

[Statement]
Welcome to reprint, but please keep the original source of the article

The first blog, I apologize
to attach the final finished map, although some functions are not very mature

Face recognition

The first is the face recognition function. Many companies have made this feature for developers to choose from. I am using the face recognition technology of Face++, but now I can only upload pictures by calling the system camera to take pictures. Analyze the picture to obtain face information. You cannot directly obtain and analyze the face while previewing the camera.
This is the Developer Center of Face++ official website, you can refer to the API document
http://www.faceplusplus.com.cn/uc_home/
This is the code for face analysis

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case PARSE_OK:

                    JSONObject rs = (JSONObject) msg.obj;
                    parseInfo(rs);
                    Log.i("MyTest", "---age-->" + age + "-----gender--" + gender + "----smiling--" + smiling);
                    emotionInfo.setVisibility(View.VISIBLE);
                    find_btn_submit.setVisibility(View.GONE);
                    emotionInfo.setText("检测结果如下:\n年龄:" + age + "\n性别:" + gender + "\n笑脸值:" + smiling);
                    noCamera.clearAnimation();
                    tips.clearAnimation();
                    tips.setText("下拉开启音乐之旅");
                    break;
                case PARSE_ERRO:
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(FindMusicActivity.this, "请检查网络连接", Toast.LENGTH_SHORT).show();
                            tips.clearAnimation();
                            tips.setText("当前网络状况出了点问题,检查一下再来吧~\n下拉随机推荐音乐");
                            noCamera.clearAnimation();
                            noCamera.setImageResource(R.mipmap.get_face_fail);
                        }
                    });
                    break;
            }
        }


    };

    /**
     * 解析心情
     */
    private void parseInfo(JSONObject obj) {
        try {
            JSONArray faces = obj.getJSONArray("face");
            int faceCounts = faces.length();
            if (faceCounts == 0) {
                Toast.makeText(FindMusicActivity.this, "不好意思,没有检测出来,请重新检测", Toast.LENGTH_SHORT).show();
                tips.clearAnimation();
                tips.setText("不好意思,没有检测出来,请长按重新检测");
                noCamera.clearAnimation();
                noCamera.setImageResource(R.mipmap.get_face_fail);
                smiling=50;
            } else {
                JSONObject face = faces.getJSONObject(0);
                String sex = face.getJSONObject("attribute").getJSONObject("gender").getString("value");
                gender = "Male".equals(sex) ? "男" : "女";
                age = face.getJSONObject("attribute").getJSONObject("age").getInt("value");
                smiling = face.getJSONObject("attribute").getJSONObject("smiling").getDouble("value");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

Play music online

The problem of playing music has been a headache for me for a long time, and I can’t find the interface. Many interfaces found on the Internet say they can be used but cannot parse the data. In the end, I still haven’t found a good interface, so I can only get it directly from the Internet. the 70 songs links stored in the class, and then loads and plays music from the network address
of songs link for the way: http://m.blog.csdn.net/blog/gikieng_tjsd/38750971
songs to play I actually wrote a friend , This process also has various problems, especially the service problem. It is always impossible to obtain the service, or the service cannot be cancelled, the music cannot be stopped, and it is not perfect until now. This is a remaining problem.

Other functions

Sidebar

The side slide bar uses the DrawLayout of the Android v4 package. I should not mention the usage of DrawLayout. I believe everyone knows it. The main thing is to pay attention to the position where it appears, otherwise an error will be reported.

gesture

There are a lot of gestures used in it, such as sliding up, down, long press, etc., which can be implemented with the Gesture class. I didn’t use this here. I used the View.OnTouchListener interface, and then in the OnTouch() method, the finger is on the screen. Touch to make a judgment. According to the different coordinates and timestamps when pressing and lifting, judge what operation is made, but this judgment is not very accurate and is not recommended.

Circular progress bar

This is a custom circular progress bar, it is a reference code to find online
we can refer to

package com.sdf.DefineView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.sdf.facemusic.R;

/**
 * Created by Administrator on 15-11-5.
 */
public class RoundProgressBar extends View
{
    
    
    // 画笔对象的引用
    private Paint paint;

    // 圆环的颜色
    private int roundColor;

    // 圆环进度的颜色

    private int roundProgressColor;

    // 中间进度百分比的字符串的颜色
    // private int textColor;

    // 中间进度百分比的字符串的字体
    // private float textSize;

    // 圆环的宽度
    private float roundWidth;

    // 最大进度
    private int max;

    // 当前进度
    private int progress;

    // 是否显示中间的进度
    //private boolean textIsDisplayable;

    // 进度的风格,实心或者空心
    private int style;

    public static final int STROKE = 0;
    public static final int FILL = 1;

    public RoundProgressBar(Context context) {
        this(context, null);
    }

    public RoundProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        paint = new Paint();

        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);

        // 获取自定义属性和默认值
        roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.WHITE);
        roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);
        roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);
        max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);
        style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);
        //回收mTypedArray
        mTypedArray.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 画最外层的大圆环
        int centre = getWidth() / 2; // 获取圆心的x坐标
        int radius = (int) (centre - roundWidth / 2); // 圆环的半径
        paint.setColor(roundColor); // 设置圆环的颜色
        paint.setStyle(Paint.Style.STROKE); // 设置空心
        paint.setStrokeWidth(roundWidth); // 设置圆环的宽度
        paint.setAntiAlias(true); // 消除锯齿
        canvas.drawCircle(centre, centre, radius, paint); // 画出圆环


        // 画圆弧 ,画圆环的进度
        // 设置进度是实心还是空心
        paint.setStrokeWidth(roundWidth); // 设置圆环的宽度
        paint.setColor(roundProgressColor); // 设置进度的颜色
        RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用于定义的圆弧的形状和大小的界限

        switch (style) {
            case STROKE: {
                paint.setStyle(Paint.Style.STROKE);
                canvas.drawArc(oval, 0, 360 * progress / max, false, paint); // 根据进度画圆弧
                break;
            }
            case FILL: {
                paint.setStyle(Paint.Style.FILL_AND_STROKE);
                if (progress != 0)
                    canvas.drawArc(oval, 0, 360 * progress / max, true, paint); // 根据进度画圆弧
                break;
            }
        }

    }

    public synchronized int getMax() {
        return max;
    }

    /**
     * 设置进度的最大值
     *
     * @param max
     */
    public synchronized void setMax(int max) {
        if (max < 0) {
            throw new IllegalArgumentException("max not less than 0");
        }
        this.max = max;
    }

    /**
     * 获取进度.需要同步
     *
     * @return
     */
    public synchronized int getProgress() {
        return progress;
    }

    /**
     * 设置进度,此为线程安全控件,由于考虑多线的问题,需要同步 刷新界面调用postInvalidate()能在非UI线程刷新
     *
     * @param progress
     */
    public synchronized void setProgress(int progress) {
        if (progress < 0) {
            throw new IllegalArgumentException("progress not less than 0");
        }
        if (progress > max) {
            progress = max;
        }
        if (progress <= max) {
            this.progress = progress;
            postInvalidate();
        }

    }

    public int getCricleColor() {
        return roundColor;
    }

    public void setCricleColor(int cricleColor) {
        this.roundColor = cricleColor;
    }

    public int getCricleProgressColor() {
        return roundProgressColor;
    }

    public void setCricleProgressColor(int cricleProgressColor) {
        this.roundProgressColor = cricleProgressColor;
    }


    public float getRoundWidth() {
        return roundWidth;
    }

    public void setRoundWidth(float roundWidth) {
        this.roundWidth = roundWidth;
    }

}

Third-party login and sharing function

The third party uses ShareSDK, I believe I have used it as a developer. When I was working on this project, because it was the first time to develop with Android studio, I didn’t know how to add it. Later, I found a friend on the Internet for a very detailed tutorial. You can refer to
http://www.cnblogs. com/smyhvae/p/4585340.html

At this point, the function of the project is probably finished. The first time I write a blog, it may be messy, forgive me.

Guess you like

Origin blog.csdn.net/lizebin_bin/article/details/49716569