我正在尝试编写一个应用程序,该应用程序必须在移动电话的屏幕上显示前置摄像头正在拍摄的内容[该应用程序不会记录/保存手机内存中的任何内容].同样在面部被拍摄(并被检测到)的情况下,它必须显示为矩形.
为此,我正在使用:
> SurfacevIEw显示前置摄像头拍摄的内容.
> FaceDetectionListener,用于检测摄像机输入中的面部.
到目前为止,该应用程序正确显示前置摄像头正在拍摄的内容.它还可以正确检测脸部.但是我无法在检测到的脸部周围绘制边界矩形.
以下是一些片段,以说明我是如何尝试解决任务的.
活动:
@Suppresslint("InflateParams")@TargetAPI(Build.VERSION_CODES.ICE_CREAM_SANDWICH)public class FaceDetectorTutorial extends Activity { MySurface mMySurface; private SurfaceVIEw surfaceVIEw; public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_face_detector_tutorial); setRequestedOrIEntation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); surfaceVIEw = (SurfaceVIEw)findVIEwByID(R.ID.camPrevIEw); mMySurface = new MySurface(this, surfaceVIEw); } }
MySurface课程:
class MySurface extends SurfaceVIEw implements SurfaceHolder.Callback { Paint paint = new Paint(); public Camera camera; String Tag = "Log: "; private SurfaceHolder surfaceHolder; boolean prevIEw = false; ////////// CLASS CONSTRUCTOR ////////// MySurface(Context context, SurfaceVIEw msurfaceVIEw) { super(context); paint.setcolor(color.RED); paint.setstrokeWIDth(3); surfaceHolder = msurfaceVIEw.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } ///////// FACE DETECTION ListENER ///////// @Suppresslint("NewAPI") FaceDetectionListener faceDetectionListener = new FaceDetectionListener(){ @OverrIDe public voID onFaceDetection(Face[] faces, Camera camera) { if (faces.length > 0){ //Just for the first one detected Rect Boundary = faces[0].rect; System.out.println(Boundary); tryDrawing(Boundary); } }};//End of FaceDetectionListener ////////// SURFACE METHODS //////// @OverrIDe public voID surfaceChanged(SurfaceHolder holder, int format, int wIDth,int height) { // Todo auto-generated method stub if(prevIEw){ camera.stopFaceDetection(); camera.stopPrevIEw(); prevIEw = false; } if (camera != null){ try { camera.setPrevIEwdisplay(surfaceHolder); camera.startPrevIEw(); camera.startFaceDetection(); prevIEw = true; } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } } } @OverrIDe public voID surfaceCreated(SurfaceHolder holder) { // Todo auto-generated method stub int cameraID = -1; int numberOfCameras = camera.getNumberOfCameras(); for (int i = 0; i < numberOfCameras; i++) { CameraInfo info = new CameraInfo(); Camera.getCameraInfo(i, info); if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { cameraID = i; break; } } camera = Camera.open(cameraID); camera.setFaceDetectionListener(faceDetectionListener); } @OverrIDe public voID surfaceDestroyed(SurfaceHolder holder) { // Todo auto-generated method stub camera.stopFaceDetection(); camera.stopPrevIEw(); camera.release(); camera = null; prevIEw = false; } private voID tryDrawing(Rect Boundary) { Log.i(Tag, "Trying to draw..."); Canvas canvas = surfaceHolder.lockCanvas(); if (canvas == null) { Log.e(Tag, "Cannot draw onto the canvas as it's null"); } else { drawMyStuff(canvas,Boundary); surfaceHolder.unlockCanvasAndPost(canvas); } } private voID drawMyStuff(final Canvas canvas, Rect Boundary) { canvas.drawRect(Boundary.left, Boundary.top, Boundary.right, Boundary.bottom, paint); Log.i(Tag, "Drawing..."); }}
布局:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:orIEntation="vertical" > <SurfaceVIEw androID:ID="@+ID/camPrevIEw" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" /></linearLayout>
从logcat我明白错误是锁定表面.但我不明白为什么.
11-23 17:13:51.791: I/System.out(12515): Rect(-187, -495 - 328, 196)11-23 17:13:51.791: I/Log:(12515): Trying to draw...11-23 17:13:51.791: E/SurfaceHolder(12515): Exception locking surface11-23 17:13:51.791: E/SurfaceHolder(12515): java.lang.IllegalArgumentException11-23 17:13:51.791: E/SurfaceHolder(12515): at androID.vIEw.Surface.nativeLockCanvas(Native Method)11-23 17:13:51.791: E/SurfaceHolder(12515): at androID.vIEw.Surface.lockCanvas(Surface.java:452)11-23 17:13:51.791: E/SurfaceHolder(12515): at androID.vIEw.SurfaceVIEw.internalLockCanvas(SurfaceVIEw.java:781)11-23 17:13:51.791: E/SurfaceHolder(12515): at androID.vIEw.SurfaceVIEw.lockCanvas(SurfaceVIEw.java:757)11-23 17:13:51.791: E/SurfaceHolder(12515): at com.example.facedetectiontutorial.MySurface.tryDrawing(FaceDetectorTutorial.java:175)11-23 17:13:51.791: E/SurfaceHolder(12515): at com.example.facedetectiontutorial.MySurface.accesssetColorFormat(TRANSLUCENT)
(FaceDetectorTutorial.java:172)11-23 17:13:51.791: E/SurfaceHolder(12515): at com.example.facedetectiontutorial.MySurface.onFaceDetection(FaceDetectorTutorial.java:109)11-23 17:13:51.791: E/SurfaceHolder(12515): at androID.harDWare.Camera$EventHandler.handleMessage(Camera.java:815)11-23 17:13:51.791: E/SurfaceHolder(12515): at androID.os.Handler.dispatchMessage(Handler.java:99)11-23 17:13:51.791: E/SurfaceHolder(12515): at androID.os.Looper.loop(Looper.java:137)11-23 17:13:51.791: E/SurfaceHolder(12515): at androID.app.ActivityThread.main(ActivityThread.java:5041)11-23 17:13:51.791: E/SurfaceHolder(12515): at java.lang.reflect.Method.invokeNative(Native Method)11-23 17:13:51.791: E/SurfaceHolder(12515): at java.lang.reflect.Method.invoke(Method.java:511)11-23 17:13:51.791: E/SurfaceHolder(12515): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)11-23 17:13:51.791: E/SurfaceHolder(12515): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:560)11-23 17:13:51.791: E/SurfaceHolder(12515): at dalvik.system.NativeStart.main(Native Method)11-23 17:13:51.893: E/Log:(12515): Cannot draw onto the canvas as it's null
我的解决方案基于以下问题的公认答案:
Android drawing on surfaceview and canvas
但似乎我错了.谁能告诉我应该改变什么?
解决方法:
你不能. Surface是生产者 – 消费者对的生产者一方,一次只能有一个生产者.您可以提供相机框架,使用Canvas在软件中渲染,或使用OpenGL在硬件中渲染,但不能将它们混合在一个Surface上.
你有几个选择.一种方法是使用与第一个重叠的第二个SurfaceVIEw(通过FrameLayout).为此,您必须为第二个Surface指定Z顺序 – 如果不这样做,系统将看到您有两个Surfaces试图占用相同的空间,只有一个将获胜.使用setZOrderMediaOverlay()方法将新Surface放置在相机预览的前面,但在VIEw UI后面.您还需要将Surface的颜色格式从默认的RGB565更改为支持透明度的格式; 或RGB8888将起作用.确保使用适当的颜色传输模式将曲面擦除为透明黑色.
另一种方法是使用custom View.这样更有效,因为它不涉及创建单独的Surface(您在VIEw UI层上绘图),Canvas渲染可以是硬件加速的.最方便的VIEw是SurfaceVIEw的一部分 – 不需要对布局进行任何更改.如果你的代码是SurfaceVIEw的子类,你已经在那里了.
有关多个重叠SurfaceVIEw的示例,请参见Grafika’s“多表面测试”活动.如果您想了解有关AndroID图形系统的更多信息,请参阅architecture doc.
总结以上是内存溢出为你收集整理的Android:如何在已经显示Camara预览版的Surfaceview上绘图全部内容,希望文章能够帮你解决Android:如何在已经显示Camara预览版的Surfaceview上绘图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)