在android摄像头界面上添加一个虚线圆圈
我正在开发一个人脸识别应用程序。我需要在我的相机界面上显示一个小的虚线圈来放置人物的脸,这样用户可以很好地训练该应用程序,而不是拍摄不同比例的照片。 我跟随了,但我不知道如何在相机界面上做到这一点。所以,任何人都可以上传一个样本项目或给我一些东西来继续我的项目吗?提前谢谢 编辑-2013-02-14在android摄像头界面上添加一个虚线圆圈,android,android-camera,android-canvas,Android,Android Camera,Android Canvas,我正在开发一个人脸识别应用程序。我需要在我的相机界面上显示一个小的虚线圈来放置人物的脸,这样用户可以很好地训练该应用程序,而不是拍摄不同比例的照片。 我跟随了,但我不知道如何在相机界面上做到这一点。所以,任何人都可以上传一个样本项目或给我一些东西来继续我的项目吗?提前谢谢 编辑-2013-02-14 好的,代码正在运行。但我还有两个问题 我会画圆圈。但它总是不在同一个位置。当屏幕大小改变时,它的位置总是在改变。我尝试了getWidt()和getHeight()方法,但我无法在屏幕中央画圆。你对此
好的,代码正在运行。但我还有两个问题
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
Camera mCamera = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.camera_layout);
mCamera = getCameraInstance();
Preview mPreview = new Preview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenCenterX = (size.x /2);
int screenCenterY = (size.y/2) ;
DrawOnTop mDraw = new DrawOnTop(this,screenCenterX,screenCenterY);
addContentView(mDraw, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//Adding listener
Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.takePicture(null, null, mPicture);
}
});
}
/**
* Helper method to access the camera returns null if
* it cannot get the camera or does not exist
* @return
*/
private Camera getCameraInstance() {
Camera camera = null;
try {
camera = Camera.open();
} catch (Exception e) {
// cannot get camera or does not exist
}
return camera;
}
PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null){
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
Toast.makeText(TestActivity.this, "Photo saved to folder \"Pictures\\MyCameraApp\"", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
private static File getOutputMediaFile(){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
return mediaFile;
}
}
class DrawOnTop extends View {
int screenCenterX = 0;
int screenCenterY = 0;
final int radius = 50;
public DrawOnTop(Context context, int screenCenterX, int screenCenterY) {
super(context);
this.screenCenterX = screenCenterX;
this.screenCenterY = screenCenterY;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
Paint p = new Paint();
p.setColor(Color.RED);
DashPathEffect dashPath = new DashPathEffect(new float[]{5,5}, (float)1.0);
p.setPathEffect(dashPath);
p.setStyle(Style.STROKE);
canvas.drawCircle(screenCenterX, screenCenterY, radius, p);
invalidate();
super.onDraw(canvas);
}
}
//----------------------------------------------------------------------
class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
Preview(Context context, Camera camera) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when
this.mCamera = camera;
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
//this is a deprecated method, is not required after 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell
// to draw.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the
// Because the CameraDevice object is not a shared resource,
// important to release it when the activity is paused.
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters
// the preview.
Camera.Parameters parameters = mCamera.getParameters();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
// You need to choose the most appropriate previewSize for your app
Camera.Size previewSize = previewSizes.get(0);
parameters.setPreviewSize(previewSize.width, previewSize.height);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.text.simpleDataFormat;
导入java.util.Date;
导入java.util.List;
导入android.app.Activity;
导入android.content.Context;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.DashPathEffect;
导入android.graphics.Paint;
导入android.graphics.Paint.Style;
导入android.graphics.Point;
导入android.hardware.Camera;
导入android.hardware.Camera.PictureCallback;
导入android.os.Bundle;
导入android.os.Environment;
导入android.util.Log;
导入android.view.Display;
导入android.view.SurfaceHolder;
导入android.view.SurfaceView;
导入android.view.view;
导入android.view.ViewGroup.LayoutParams;
导入android.view.Window;
导入android.widget.Button;
导入android.widget.FrameLayout;
导入android.widget.Toast;
公共类测试活动扩展活动{
/**首次创建活动时调用。*/
摄像机mCamera=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
setContentView(R.layout.camera_layout);
mCamera=getCameraInstance();
Preview mPreview=新预览(此为mCamera);
FrameLayout preview=(FrameLayout)findviewbyd(R.id.camera\u preview);
preview.addView(mPreview);
Display Display=getWindowManager().getDefaultDisplay();
点大小=新点();
display.getSize(size);
int screenCenterX=(大小为.x/2);
int screensentery=(size.y/2);
DrawOnTop mDraw=新的DrawOnTop(此、screenCenterX、screenCenterY);
addContentView(mDraw,新的LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容));
//添加侦听器
按钮捕获按钮=(按钮)findViewById(R.id.Button\u捕获);
captureButton.setOnClickListener(
新建视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
takePicture(null,null,mPicture);
}
});
}
/**
*用于访问摄影机的帮助器方法在以下情况下返回null
*它无法获取相机或不存在
*@返回
*/
私人摄像机getCameraInstance(){
摄像头=空;
试一试{
camera=camera.open();
}捕获(例外e){
//无法获取相机或相机不存在
}
返回摄像机;
}
PictureCallback mpacture=新建PictureCallback(){
@凌驾
公共void onPictureTaken(字节[]数据,摄像头){
File pictureFile=getOutputMediaFile();
如果(pictureFile==null){
返回;
}
试一试{
FileOutputStream fos=新的FileOutputStream(pictureFile);
写入(数据);
fos.close();
Toast.makeText(TestActivity.this,“照片保存到文件夹\“Pictures\\MyCameraApp\”,Toast.LENGTH\u SHORT.show();
}catch(filenotfounde异常){
}捕获(IOE异常){
}
}
};
私有静态文件getOutputMediaFile(){
File mediaStorageDir=新文件(Environment.getExternalStoragePublicDirectory(
环境。目录(图片),“MyCameraApp”);
如果(!mediaStorageDir.exists()){
如果(!mediaStorageDir.mkdirs()){
Log.d(“MyCameraApp”,“创建目录失败”);
返回null;
}
}
//创建媒体文件名
字符串时间戳=新的SimpleDateFormat(“yyyyMMdd_HHmmss”)。格式(新日期();
文件媒体文件;
mediaFile=新文件(mediaStorageDir.getPath()+File.separator+
“IMG_”+时间戳+”.jpg”);
返回媒体文件;
}
}
类DrawOnTop扩展视图{
int screensenterx=0;
int screensentery=0;
最终整数半径=50;
公共DrawOnTop(上下文上下文,int-screenCenterX,int-screenCenterY){
超级(上下文);
this.screenCenterX=screenCenterX;
this.screensentery=screensentery;
}
@凌驾
受保护的void onDraw(画布){
//TODO自动生成的方法存根
油漆p=新油漆();
p、 setColor(Color.RED);
DashPathEffect dashPath=新DashPathEffect(新浮点[]{5,5},(浮点)1.0);
p、 setPathEffect(dashPath);
p、 设置样式(样式笔划);
画布绘制圆(屏幕中心X、屏幕中心Y、半径p);
使无效();
super.onDraw(帆布);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
<Button
android:id="@+id/button_capture"
android:text="Capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />