package com.example.devin.myapplication;
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.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private ImageView imageview;
private TextView countertext;
String result = null;
String TAG = "HttpPort";
private int SCEEEN_WIDTH, SCEEEN_HIGHT;
byte[] buffer = new byte[1024*16];
private int TIME = 100;
private int numberOfFace = 5; //最大检测的人脸数
private FaceDetector myFaceDetect; //人脸识别类的实例
private FaceDetector.Face[] myFace; //存储多张人脸的数组变量
private BitmapFactory.Options BitmapFactoryinfo;
float myEyesDistance; //两眼之间的距离
int numberOfFaceDetected; //实际检测到的人脸数
private Bitmap image;
private Handler handler;
HttpThreadGet myHttpThreadGet;
// HttpThreadPost myHttpThreadPost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
widgetSetup();
paramsSetup();
detectSetup();
//faceDetect(image);
// timerHandler.postDelayed(runnable, TIME); //每隔1s执行
/************************** msg接收 *************************/
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case HttpThreadPost.POST:
result = (String) msg.obj;
break;
case HttpThreadGet.GET:
result = (String) msg.obj;
break;
case HttpThreadGet.GETIMG:
buffer = (byte[])msg.obj;
try {
image = BitmapFactory.decodeByteArray(buffer, 0, buffer.length, BitmapFactoryinfo);
faceDetect(image);
} catch (Exception e) {
Log.e(TAG, e.toString());
}
doGet(); //再请求
break;
default:
break;
}
}
};
doGet();
}
/************************** face detect *************************/
private void faceDetect(Bitmap fBitmap) {
// myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.baby, BitmapFactoryOptionsbfo);
int imageWidth = fBitmap.getWidth();
int imageHeight = fBitmap.getHeight();
myFace = new FaceDetector.Face[numberOfFace]; //分配人脸数组空间
myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
numberOfFaceDetected = myFaceDetect.findFaces(fBitmap, myFace); //FaceDetector 构造实例并解析人脸
countertext.setText("numberOfFaceDetected is " + numberOfFaceDetected);
Log.i(TAG,"numberOfFaceDetected is " + numberOfFaceDetected);
Bitmap bitmapTemp = Bitmap.createBitmap(fBitmap.getWidth(), fBitmap.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmapTemp);
canvas.drawColor(Color.TRANSPARENT);
Paint myPaint = new Paint();
myPaint.setColor(Color.GREEN);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(3); //设置位图上paint操作的参数
canvas.drawBitmap(fBitmap, 0, 0, myPaint);
for(int i=0; i < numberOfFaceDetected; i++){
Face face = myFace[i];
PointF myMidPoint = new PointF();
face.getMidPoint(myMidPoint);
myEyesDistance = face.eyesDistance(); //得到人脸中心点和眼间距离参数,并对每个人脸进行画框
canvas.drawRect( //矩形框的位置参数
(int)(myMidPoint.x - myEyesDistance),
(int)(myMidPoint.y - myEyesDistance),
(int)(myMidPoint.x + myEyesDistance),
(int)(myMidPoint.y + myEyesDistance),
myPaint);
}
imageview.setImageBitmap(bitmapTemp);
}
/************************** register widget *************************/
private void widgetSetup() {
imageview = (ImageView) findViewById(R.id.imageView);
countertext = (TextView) findViewById(R.id.textView);
}
/************************** get HW & set widget size *************************/
private void paramsSetup() {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
SCEEEN_WIDTH = dm.widthPixels;
SCEEEN_HIGHT = dm.heightPixels;
ViewGroup.LayoutParams para;
para = imageview.getLayoutParams();
para.width = (int)(SCEEEN_WIDTH * 0.96);
para.height = (int)(SCEEEN_WIDTH * 0.75 * 0.96);
imageview.setLayoutParams(para);
}
/************************** format bitmap *************************/
private void detectSetup() {
BitmapFactoryinfo = new BitmapFactory.Options();
BitmapFactoryinfo.inPreferredConfig = Bitmap.Config.RGB_565; //构造位图生成的参数,必须为565。类名+enum
}
private void doPost() {
//TODO
}
private void doGet() {
//get http img
String url = "http://192.168.0.105:8080/?action=snapshot";
// Log.d(TAG, url);
myHttpThreadGet = new HttpThreadGet(url, HttpThreadGet.GETIMG, handler);
myHttpThreadGet.start();
}
// /************************** 定时器发送 *************************/
//
// Handler timerHandler = new Handler();
// Runnable runnable = new Runnable() {
//
// @Override
// public void run() {
// // handler自带方法实现定时器
// try {
// handler.postDelayed(this, TIME);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// };
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy() {
super.onDestroy();
if(myHttpThreadGet.isAlive()) {
myHttpThreadGet.interrupt();
}
}
}
大囚长
- 粉丝: 1121
- 资源: 48
最新资源
- 1734956384252.jpeg
- NEMS.exe v2.4
- 游戏人物头部检测24-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 疾病防控综合系统-JAVA-基于springboot的疾病防控综合系统的设计与实现(毕业论文)
- VBA视频教程 0005
- 游戏人物检测1-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 四轮独立驱动横摆角速度控制,LQR 基于LQR算法的 基于二自由度动力学方程,通过主动转向afs和直接横摆力矩dyc实现的横摆角速度跟踪 ,模型包括期望横摆角速度,质心侧偏角,稳定性因素,lqr模块等
- 线控转向系统Carsim和Simulink联合仿真模型,带Carsim数据库,C级车 【正向建模,利用三环PID控制算法控制无刷直流电机获得前轮转角】 主要根据Carsim自带的转向系统,查出小齿轮
- Survey Document -1.xlsx
- 游戏人物检测11-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 线控转向-Carsim与simulink联合仿真模型 包含转向电机模型,转向执行机构模型,齿轮齿条模型 提供carsim参数配置文件 simulink模型文件 对应参考资料
- 数据结构课设-哈夫曼编码译码器
- 基于一阶RC模型,电池带遗忘因子递推最小二乘法+扩展卡尔曼滤波算法(FFRLS+ EKF),参数与SOC的在线联合估计,matlab程序
- 车牌号自动识别程序-matlab
- 游戏人物检测129-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 考虑过网费用分摊的多产消者点对点能源交易分布式优化 摘要:代码主要做的是配电网中产消者点对点交易相关研究,配网中的卖方和买方通过P2P交易匹配协商来平衡供需,同时重点考虑了P2P交易过程中公共设施的使
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈