我听说android studio上已经有一个相机类了。
我试图做的是我试图制作一个应用程序,当按下它时它打开相机和它唯一的前置摄像头,然后当它自动检测到它时,它会说出像#34;你今天看起来很棒!"我想录制自己的声音并将其作为我自己的声音。
因此,如果有人能指导我如何做这样的事情,那将是惊人的!我到处研究过!
我已经下载了这个已经制作好的面部检测界面。它的工作原理,但我现在真正需要做的就是加入我的声音。
这就是我真正想要的!你能给我一些建议或至少给我一些步骤怎么做?
我是一个菜鸟,所以在解释时要轻松一下
答案 0 :(得分:1)
只需使用原生Android的面部检测库(而不是OpenCV或更复杂的东西),一旦应用程序启动,也会跟踪您的脸部。 您还可以使用if语句检查是否识别了面部以及是否已播放声音消息。
if(recognized_faces != 0 && message_already_played == false){
//play the message
}
这是一段代码(活动),它从正面相机拍摄所有照片并分析它们。最初在摄影图上用画布绘制的代码并按顺序显示它们(就像它是视频流一样)到" miaImmagine2"。如果你不想要它,就不要显示miaImmagine2并只显示摄像机图层。我已经删除了您不需要的许多代码部分,因此可能需要擦除或调整大量代码。但它对你来说可能是一个很好的帮助。所以这是代码:
package mawashi.alex.driveawake;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.hardware.Camera.ErrorCallback;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.Size;
import android.media.AudioManager;
import android.media.FaceDetector;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
@SuppressLint("NewApi")
public class AwakeActivity extends Activity {
SurfaceView mVideoCaptureView;
Camera mCamera;
public Bitmap mFaceBitmap;
public TextView Testo;
public Bitmap mFace2Bitmap;
public ImageView miaImmagine2;
private MediaPlayer player;
public SurfaceView mSurfaceView;
public SurfaceHolder mSurfaceHolder;
private static final int MAX_FACES = 1;
int mFaceWidth;
int mFaceHeight;
int cameraType = 1; // front
int frame_sec = 1000000;
byte[] callbackBuffer;
PreviewCallback cb;
public FrameLayout preview;
Bitmap bmp;
Bitmap b;
MediaMetadataRetriever mediaMetadataRetriever=null;
Bitmap bmFrame = null;
FaceDetector.Face[] faces;
Activity activity;
Context mContext;
int index_sleep = 0;
int consec = 0;
boolean sleep = false;
public float scala = 1;
public int offset = 0;
int m = 1;
int Colore = Color.GRAY;
private MediaPlayer player2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.detection);
mVideoCaptureView = (SurfaceView) findViewById(R.id.Surface);
miaImmagine2 = (ImageView) findViewById(R.id.imageView2);
Testo = (TextView) findViewById(R.id.textView1);
SurfaceHolder videoCaptureViewHolder = mVideoCaptureView.getHolder();
videoCaptureViewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
player = MediaPlayer.create(this, R.raw.you_look_good);
videoCaptureViewHolder.addCallback(new Callback() {
public void surfaceDestroyed(SurfaceHolder holder) {
}
public void surfaceCreated(SurfaceHolder holder) {
startVideo();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
});
}
////////////////////////////////////////////////////////////////////////////////////////
//************BUTTONS***************************************************************
//ROTATE THE IMAGE OF 90 DEGREES
protected void setDisplayOrientation(Camera mCamera, int angle){
Method downPolymorphic;
try{
downPolymorphic = mCamera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });
if (downPolymorphic != null)
downPolymorphic.invoke(mCamera, new Object[] { angle });
}catch (Exception e1){ }
}
/////////////////////////////////////////////////////////////////////////////////////////////
//******************************************************************************************
@Override
public void onPause(){
super.onPause();
player.pause();
}
//**************************************************************************************
//*******************************STARTVIDEO*********************************************
@SuppressLint("NewApi")
private void startVideo() {
SurfaceHolder videoCaptureViewHolder = null;
try {
mCamera = Camera.open(1);
} catch (RuntimeException e) {
Log.e("CameraTest", "Camera Open filed");
return;
}
mCamera.setErrorCallback(new ErrorCallback() {
public void onError(int error, Camera camera) {
}
});
Camera.Parameters parameters = mCamera.getParameters();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
Camera.Size cs = previewSizes.get(0);
parameters.setPreviewSize(cs.width,cs.height);
setDisplayOrientation(mCamera, 90);
mCamera.setParameters(parameters);
if (null != mVideoCaptureView)
videoCaptureViewHolder = mVideoCaptureView.getHolder();
try {
mCamera.setPreviewDisplay(videoCaptureViewHolder);
} catch (Throwable t) {
}
Log.v("CameraTest","Camera PreviewFrameRate = "+mCamera.getParameters().getPreviewFrameRate());
Size previewSize=mCamera.getParameters().getPreviewSize();
int dataBufferSize=(int)(previewSize.height*previewSize.width* (ImageFormat.getBitsPerPixel(mCamera.getParameters().getPreviewFormat())/8.0));
mCamera.addCallbackBuffer(new byte[dataBufferSize]);
mCamera.setPreviewCallbackWithBuffer(new Camera.PreviewCallback() {
private long timestamp=0;
public synchronized void onPreviewFrame(byte[] data, Camera camera) {
Size previewSize=camera.getParameters().getPreviewSize();
YuvImage yuvImage= new YuvImage(data,ImageFormat.NV21, previewSize.width, previewSize.height, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, previewSize.width, previewSize.height),80, baos);
byte jpgData[]=baos.toByteArray();
bmp = BitmapFactory.decodeByteArray(jpgData, 0, jpgData.length);
Bitmap bmp2 = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas=new Canvas(bmp2);
Paint paint=new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
Matrix matrix = new Matrix();
matrix.setRotate(270,bmp.getWidth()/2,bmp.getHeight()/2);
canvas.drawBitmap(bmp, matrix, paint);
faces = new FaceDetector.Face[MAX_FACES];
int mFaceWidth = bmp2.getWidth();
int mFaceHeight = bmp2.getHeight();
PointF midPoint = new PointF(); //inizializza Punto di coordinate float
FaceDetector detector = new FaceDetector(mFaceWidth, mFaceHeight,MAX_FACES);
int facesFound = detector.findFaces(bmp2, faces); //bmp
if(facesFound > 0 && message_started==false){
faces[0].getMidPoint(midPoint);
float eyeDistance = faces[0].eyesDistance();
float confidence = faces[0].confidence();
player.start(); //play of "You look good today!"
message_started = true;
}
miaImmagine2.setImageBitmap(bmp2);
//+++++++++++++++++++++PARTE FINALE++++++++++++++++++++++++++++++++++++++++
try{
camera.addCallbackBuffer(data);
}catch (Exception e) {
Log.e("CameraTest", "addCallbackBuffer error");
return;
}
return;
}
});
try {
mCamera.startPreview();
} catch (Throwable e) {
mCamera.release();
mCamera = null;
return;
}
}
//*************************************STARTVIDEO_END***************************************
////////////////////////////////////////////////////////////////////////////////////////////
//*************************************STOPVIDEO********************************************
private void stopVideo() {
if(null==mCamera)
return;
try {
mCamera.stopPreview();
mCamera.setPreviewDisplay(null);
mCamera.setPreviewCallbackWithBuffer(null);
mCamera.release();
} catch (IOException e) {
e.printStackTrace();
return;
}
mCamera = null;
}
//**********************************STOPVIDEO_END********************************************
//////////////////////////////////////////////////////////////////////////////////////////////
//**********************************FINISH***************************************************
public void finish(){
stopVideo();
super.finish();
};
//**********************************FINISH_END***********************************************
}