From dc49680d28bffdba2168671e967e8a11988960ba Mon Sep 17 00:00:00 2001 From: xuxiuxi <xuxiuxi@454eff88-639b-444f-9e54-f578c98de674> Date: 星期五, 21 七月 2017 09:27:30 +0800 Subject: [PATCH] --- VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/base/MainActivity.java | 280 +++++++++++++++++++++++++++---------------------------- 1 files changed, 138 insertions(+), 142 deletions(-) diff --git a/VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/base/MainActivity.java b/VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/base/MainActivity.java index 40d4ed5..cda3df0 100644 --- a/VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/base/MainActivity.java +++ b/VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/base/MainActivity.java @@ -44,10 +44,12 @@ import com.luxand.FSDK; import cn.com.basic.face.discern.common.CommonVariables; +import cn.com.basic.face.discern.entity.PhoneCall; import cn.com.basic.face.discern.query.item.SurveillanceQueryItem; import cn.com.basic.face.fragment.AttenderRegisterFragment; import cn.com.basic.face.fragment.SurveillanceFragment; import cn.com.basic.face.service.SurveillanceMng; +import cn.com.basic.face.service.sqlite.SurveillanceDao; import cn.com.basic.face.service.sqlite.TestDao; import cn.com.basic.face.util.FileUtil; import cn.com.basic.face.util.NativeImg; @@ -69,6 +71,43 @@ import java.util.List; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; +import android.view.ViewGroup.LayoutParams; + +import com.luxand.FSDK; +import com.luxand.FSDK.HTracker; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.util.List; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; +import android.app.Activity; +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.pm.ActivityInfo; +import android.content.res.Configuration; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Paint.Align; +import android.graphics.drawable.ColorDrawable; +import android.hardware.Camera; +import android.hardware.Camera.PreviewCallback; +import android.hardware.Camera.Size; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.MotionEvent; +import android.view.SurfaceHolder; +import android.view.SurfaceView; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.Window; +import android.view.WindowManager; +import android.view.ViewGroup.LayoutParams; +import android.widget.EditText; public class MainActivity extends BaseActivity implements View.OnClickListener{ @@ -340,17 +379,25 @@ if (CommonVariables.FACE_SERVER) { sDensity = getResources().getDisplayMetrics().scaledDensity; - int res = 0; + String s = FileUtil.getStringFromFile("/system/a"); + int res = FSDK.ActivateLibrary(s); if (res != FSDK.FSDKE_OK) { mIsFailed = true; showErrorAndClose("failed", res); } else { FSDK.Initialize(); + + // Hide the window title (it is done in manifest too) getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); + //requestWindowFeature(Window.FEATURE_NO_TITLE); + + // Lock orientation setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + + // Camera layer and drawing layer mDraw = new ProcessImageAndDrawResults(this); mPreview = new Preview(this, mDraw); - mDraw.mTracker = new FSDK.HTracker(); + mDraw.mTracker = new HTracker(); String templatePath = this.getApplicationInfo().dataDir + "/" + database; if (FSDK.FSDKE_OK != FSDK.LoadTrackerMemoryFromFile(mDraw.mTracker, templatePath)) { res = FSDK.CreateTracker(mDraw.mTracker); @@ -358,10 +405,40 @@ showErrorAndClose("Error creating tracker", res); } } + resetTrackerParameters(); + + this.getWindow().setBackgroundDrawable(new ColorDrawable()); //black background //setContentView(mPreview); //creates MainActivity contents //addContentView(mDraw, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); + + + // Menu +// LayoutInflater inflater = (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE ); +// View buttons = inflater.inflate(R.layout.bottom_menu, null ); +// buttons.findViewById(R.id.helpButton).setOnClickListener(this); +// buttons.findViewById(R.id.clearButton).setOnClickListener(this); +// addContentView(buttons, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + + +// FSDK.Initialize(); +// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); +// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); +// mDraw = new ProcessImageAndDrawResults(this); +// mPreview = new Preview(this, mDraw); +// mDraw.mTracker = new FSDK.HTracker(); +// String templatePath = this.getApplicationInfo().dataDir + "/" + database; +// if (FSDK.FSDKE_OK != FSDK.LoadTrackerMemoryFromFile(mDraw.mTracker, templatePath)) { +// res = FSDK.CreateTracker(mDraw.mTracker); +// if (FSDK.FSDKE_OK != res) { +// showErrorAndClose("Error creating tracker", res); +// } +// } +// resetTrackerParameters(); +// +// setContentView(mPreview); //creates MainActivity contents +// addContentView(mDraw, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); // Menu // LayoutInflater inflater = (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE ); @@ -466,6 +543,11 @@ mDraw.mStopping = 0; } + @Override + protected void onStop() { + super.onStop(); + Config.sqlMap.getDb().close(); + } } @@ -474,8 +556,9 @@ public int x1, y1, x2, y2; } +// Draw graphics on top of the video class ProcessImageAndDrawResults extends View { - public FSDK.HTracker mTracker; + public HTracker mTracker; final int MAX_FACES = 5; final FaceRectangle[] mFacePositions = new FaceRectangle[MAX_FACES]; @@ -533,12 +616,12 @@ mPaintGreen.setStyle(Paint.Style.FILL); mPaintGreen.setColor(Color.GREEN); mPaintGreen.setTextSize(18 * MainActivity.sDensity); - mPaintGreen.setTextAlign(Paint.Align.CENTER); + mPaintGreen.setTextAlign(Align.CENTER); mPaintBlue = new Paint(); mPaintBlue.setStyle(Paint.Style.FILL); mPaintBlue.setColor(Color.BLUE); mPaintBlue.setTextSize(18 * MainActivity.sDensity); - mPaintBlue.setTextAlign(Paint.Align.CENTER); + mPaintBlue.setTextAlign(Align.CENTER); mPaintBlueTransparent = new Paint(); mPaintBlueTransparent.setStyle(Paint.Style.STROKE); @@ -546,13 +629,15 @@ mPaintBlueTransparent.setColor(Color.BLUE); mPaintBlueTransparent.setTextSize(25); + //mBitmap = null; mYUVData = null; mRGBData = null; first_frame_saved = false; } - public void onDraw(Canvas canvas) { + @Override + protected void onDraw(Canvas canvas) { if (mStopping == 1) { mStopped = 1; super.onDraw(canvas); @@ -673,7 +758,11 @@ bitmapList.add(surveillanceQueryItem); - SurveillanceMng.getInstance().addBitmap(bitmapList, nativeImg.image, 0, CommonVariables.Camera.IN+"", nativeImg.image); + if (BaseApplication.getInstance().networkAvailable()) { + SurveillanceMng.getInstance().addBitmap(bitmapList, nativeImg.image, 0, CommonVariables.Camera.IN+"", nativeImg.image); + } else { + SurveillanceDao.getInstance().saveOfflineUnknownToLocal(bitmapList, nativeImg.image, CommonVariables.Camera.IN+""); + } } } @@ -700,7 +789,7 @@ } super.onDraw(canvas); - } + } // end onDraw method @Override @@ -715,6 +804,10 @@ long IDs[] = new long[MAX_FACES]; for (int i=0; i<MAX_FACES; ++i) { rects[i] = new FaceRectangle(); + if(rects[i] == null) { + faceLock.unlock(); + return true; + } rects[i].x1 = mFacePositions[i].x1; rects[i].y1 = mFacePositions[i].y1; rects[i].x2 = mFacePositions[i].x2; @@ -729,6 +822,7 @@ mTouchedIndex = i; + // requesting name on tapping the face final EditText input = new EditText(mContext); AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setMessage("Enter person's name" ) @@ -746,7 +840,7 @@ mTouchedIndex = -1; } }) - .setCancelable(false) + .setCancelable(false) // cancel with button only .show(); break; @@ -783,121 +877,12 @@ } } } - - public void startFaceDetection() { - if (mStopping == 1) { - mStopped = 1; - return; - } - - if (mYUVData == null || mTouchedIndex != -1) { - return; - } - - - decodeYUV420SP(mRGBData, mYUVData, mImageWidth, mImageHeight); - - FSDK.HImage Image = new FSDK.HImage(); - FSDK.FSDK_IMAGEMODE imagemode = new FSDK.FSDK_IMAGEMODE(); - imagemode.mode = FSDK.FSDK_IMAGEMODE.FSDK_IMAGE_COLOR_24BIT; - FSDK.LoadImageFromBuffer(Image, mRGBData, mImageWidth, mImageHeight, mImageWidth*3, imagemode); - FSDK.MirrorImage(Image, false); - FSDK.HImage RotatedImage = new FSDK.HImage(); - FSDK.CreateEmptyImage(RotatedImage); - - int ImageWidth = mImageWidth; - if (rotated) { - ImageWidth = mImageHeight; - FSDK.RotateImage90(Image, -1, RotatedImage); - } else { - FSDK.CopyImage(Image, RotatedImage); - } - - //String surveillancePhotoName11 = "/storage/sdcard1/"+System.currentTimeMillis()+".jpg"; - //FSDK.SaveImageToFile(Image, surveillancePhotoName11); - - - FSDK.FreeImage(Image); - - long IDs[] = new long[MAX_FACES]; - long face_count[] = new long[1]; - - - - FSDK.FeedFrame(mTracker, 0, RotatedImage, face_count, IDs); - - System.out.println("face_count="+face_count[0]+", mImageWidth="+mImageWidth+", mImageHeight="+mImageHeight); - - faceLock.lock(); - - for (int i=0; i<MAX_FACES; ++i) { - mFacePositions[i] = new FaceRectangle(); - mFacePositions[i].x1 = 0; - mFacePositions[i].y1 = 0; - mFacePositions[i].x2 = 0; - mFacePositions[i].y2 = 0; - mIDs[i] = IDs[i]; - } - - - for (int i = 0; i < (int)face_count[0]; ++i) { - FSDK.FSDK_Features Eyes = new FSDK.FSDK_Features(); - FSDK.GetTrackerEyes(mTracker, 0, mIDs[i], Eyes); - - GetFaceFrame(Eyes, mFacePositions[i]); - } - if ((int)face_count[0] > 0) { - - FSDK.HImage img2 = new FSDK.HImage(); - FSDK.CreateEmptyImage(img2); - FSDK.TFacePosition fp = new FSDK.TFacePosition(); - FSDK.DetectFace(RotatedImage, fp); - - int x1 = (int)(fp.xc - 1.2*fp.w/2); - int y1 = (int)(fp.yc - 1.4*fp.w/2); - int x2 = (int)(fp.xc + 1.2*fp.w/2); - int y2 = (int)(fp.yc + 1.4*fp.w/2); - - FSDK.CopyRect(RotatedImage, x1, y1, x2, y2, img2); - - - System.out.println("face_count="+face_count[0]); - String surveillancePhotoName = MainActivity.getInstance().getFilesDir().getAbsolutePath()+System.currentTimeMillis()+".jpg"; - FSDK.SaveImageToFile(img2, surveillancePhotoName); - - List bitmapList = new ArrayList(); - for (int i = 0; i < 1; i++) { - NativeImg nativeImg = new NativeImg(); - nativeImg.image = FileUtil.readFile(new File(surveillancePhotoName)); - nativeImg.size = nativeImg.image.length; - SurveillanceQueryItem surveillanceQueryItem = new SurveillanceQueryItem(); - surveillanceQueryItem.setNativeImg(nativeImg); - nativeImg.st_track_id = 1; - surveillanceQueryItem.setStTrackId(nativeImg.st_track_id); - BitmapFactory.Options options = new BitmapFactory.Options(); - options.inMutable = true; - Bitmap bmp = BitmapFactory.decodeByteArray(nativeImg.image, 0, nativeImg.image.length, options); - Canvas canvas1 = new Canvas(bmp); - - surveillanceQueryItem.setBitmap(bmp); - surveillanceQueryItem.setWidth("100"); - surveillanceQueryItem.setHeight("100"); - bitmapList.add(surveillanceQueryItem); - - - SurveillanceMng.getInstance().addBitmap(bitmapList, nativeImg.image, 0, CommonVariables.Camera.IN+"", nativeImg.image); - } +} // end of ProcessImageAndDrawResults class - } - FSDK.FreeImage(RotatedImage); - faceLock.unlock(); - - } -} - +// Show video from camera and pass frames to ProcessImageAndDraw class class Preview extends SurfaceView implements SurfaceHolder.Callback { Context mContext; SurfaceHolder mHolder; @@ -910,48 +895,46 @@ mContext = context; mDraw = draw; + //Install a SurfaceHolder.Callback so we get notified when the underlying surface is created and destroyed. mHolder = getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); - - //new MyThread1().start(); } - int count = 0; + //SurfaceView callback public void surfaceCreated(SurfaceHolder holder) { mFinished = false; + // Find the ID of the camera int cameraId = 0; boolean frontCameraFound = false; Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); for (int i = 0; i < Camera.getNumberOfCameras(); i++) { Camera.getCameraInfo(i, cameraInfo); + //if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { cameraId = i; frontCameraFound = true; } } - //If authorisation not granted for camera - if (ContextCompat.checkSelfPermission(MainActivity.getInstance(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) - //ask for authorisation - ActivityCompat.requestPermissions(MainActivity.getInstance(), new String[]{Manifest.permission.CAMERA}, 50); + if (frontCameraFound) { mCamera = Camera.open(cameraId); } else { mCamera = Camera.open(); } + try { mCamera.setPreviewDisplay(holder); - mCamera.addCallbackBuffer(new byte[((640 * 480) * ImageFormat.getBitsPerPixel(ImageFormat.NV21)) / 8]); - - //mCamera.setPreviewCallback - mCamera.setPreviewCallbackWithBuffer(new Camera.PreviewCallback() { - public void onPreviewFrame(final byte[] data, final Camera camera) { - mCamera.addCallbackBuffer(data); + // Preview callback used whenever new viewfinder frame is available + mCamera.setPreviewCallback(new PreviewCallback() { + public void onPreviewFrame(byte[] data, Camera camera) { if ( (mDraw == null) || mFinished ) return; + if (mDraw.mYUVData == null) { + // Initialize the draw-on-top companion Camera.Parameters params = camera.getParameters(); mDraw.mImageWidth = params.getPreviewSize().width; mDraw.mImageHeight = params.getPreviewSize().height; @@ -959,6 +942,7 @@ mDraw.mYUVData = new byte[data.length]; } + // Pass YUV data to draw-on-top companion System.arraycopy(data, 0, mDraw.mYUVData, 0, data.length); mDraw.invalidate(); } @@ -981,7 +965,11 @@ } } + //SurfaceView callback public void surfaceDestroyed(SurfaceHolder holder) { + // Surface will be destroyed when we return, so stop the preview. + // Because the CameraDevice object is not a shared resource, it's very + // important to release it when the activity is paused. mFinished = true; if (mCamera != null) { mCamera.setPreviewCallback(null); @@ -991,24 +979,32 @@ } } + //SurfaceView callback, configuring camera public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { if (mCamera == null) return; + // Now that the size is known, set up the camera parameters and begin + // the preview. Camera.Parameters parameters = mCamera.getParameters(); + //Keep uncommented to work correctly on phones: + //This is an undocumented although widely known feature + /**/ if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { parameters.set("orientation", "portrait"); - mCamera.setDisplayOrientation(90); + mCamera.setDisplayOrientation(90); // For Android 2.2 and above mDraw.rotated = true; } else { parameters.set("orientation", "landscape"); - mCamera.setDisplayOrientation(0); + mCamera.setDisplayOrientation(0); // For Android 2.2 and above } - List<Camera.Size> supportedSizes = parameters.getSupportedPreviewSizes(); + /**/ + + // choose preview size closer to 640x480 for optimal performance + List<Size> supportedSizes = parameters.getSupportedPreviewSizes(); int width = 0; int height = 0; - System.out.println("surfaceChanged"); - for (Camera.Size s: supportedSizes) { + for (Size s: supportedSizes) { if ((width - 640)*(width - 640) + (height - 480)*(height - 480) > (s.width - 640)*(s.width - 640) + (s.height - 480)*(s.height - 480)) { width = s.width; @@ -1016,10 +1012,12 @@ } } + //try to set preferred parameters try { if (width*height > 0) { parameters.setPreviewSize(width, height); } + //parameters.setPreviewFrameRate(10); parameters.setSceneMode(Camera.Parameters.SCENE_MODE_PORTRAIT); parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); mCamera.setParameters(parameters); @@ -1033,7 +1031,7 @@ } private void makeResizeForCameraAspect(float cameraAspectRatio){ - ViewGroup.LayoutParams layoutParams = this.getLayoutParams(); + LayoutParams layoutParams = this.getLayoutParams(); int matchParentWidth = this.getWidth(); int newHeight = (int)(matchParentWidth/cameraAspectRatio); if (newHeight != layoutParams.height) { @@ -1043,6 +1041,4 @@ this.invalidate(); } } - -} - +} // end of Preview class -- Gitblit v1.8.0