这几天看了很多大神写的在surfaceView上添加一个矩形框的功能 ,我自己也写了一个类似的简单功能实现,目前还有个技术难题没有实现,有那个大神看到这篇文章麻烦现身指点一二,别的不多说了 代码先贴出来,问题后面在问!
先写一个相机类:(我有一部分是用afinal快速框架开发的,比较方便,推荐大家都使用下!)
package com.example.cameratest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
public class CameraActivity extends FinalActivity {
@ViewInject(id = R.id.buttonLayout)View layout;
@ViewInject(id = R.id.surfaceview,click = "focusing")SurfaceView surfaceView;
private Camera camera;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
// layout.getBackground().setAlpha(150);//这里可以给背景添加透明度
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().setFixedSize(640, 480);
surfaceView.getHolder().setKeepScreenOn(true);
surfaceView.getHolder().addCallback(new SurfaceCallBack());
}
/**
* 拍照点击事件
* @param view
*/
public void takepicture(View view){
if(camera!=null){
camera.takePicture(null, null, new MypictureCallback());
}
}
/**
* 聚焦点击事件
* @param view
*/
public void focusing(View view){
if(camera!=null){
camera.autoFocus(new MyAutoFocusCallback());
}
}
/**
* 聚焦点击事件内部类
* @author wanglei
*
*/
private final class MyAutoFocusCallback implements AutoFocusCallback{
@Override
public void onAutoFocus(boolean success, Camera camera) {
// TODO Auto-generated method stub
if(success){
Log.i("main", "对焦完成");
camera.takePicture(null, null, new MypictureCallback());
}else{
Log.i("main", "对焦else");
}
}
}
/**
* 拍照事件内部类
* @author wanglei
*
*/
private final class MypictureCallback implements PictureCallback{
@Override
/**
* @param data 传过来的jpg
* @param camera 摄像头
*/
public void onPictureTaken(byte[] data, Camera camera) {
try {
File jpgFile = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
FileOutputStream outputStream = new FileOutputStream(jpgFile);
outputStream.write(data);
outputStream.close();
camera.startPreview();//开始预览
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 打开相机内部类
* @author wanglei
*
*/
private final class SurfaceCallBack implements Callback{
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera = Camera.open(); //打开摄像头
Camera.Parameters parameters = camera.getParameters();
// Log.i("main", "摄像头的各项参数:"+parameters.flatten());
parameters.setPreviewSize(640, 480);//摄像头的像素
// parameters.setPreviewFrameRate(5);//摄像头每秒获得5个画面
parameters.setPictureSize(640, 480);//拍摄出照片的大小
parameters.setPictureFormat(PixelFormat.JPEG);
parameters.setJpegQuality(100);//jpg图片的质量
parameters.setRotation(90);//相机旋转90度
camera.setParameters(parameters);//把参数设回给摄像头
camera.setPreviewDisplay(holder);//设置显示摄像头捕获的画面
camera.setDisplayOrientation(90);//旋转90度解决相机反向问题
camera.startPreview();//开始预览
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
/**
* 关闭摄像头
*/
public void surfaceDestroyed(SurfaceHolder holder) {
if(camera!=null){
camera.stopPreview();
camera.release();
camera = null;
Log.i("main", "摄像头关闭");
}
}
}
}
然后是在上面层的view类:
package com.example.cameratest;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class MyView extends View {
private int x = 50;
private int y = 50;
private int w = 210;
private int h = 150;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
setLongClickable(true);
this.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//就是这里 各位大神 我想写个类似于系统自带的图片裁剪框的那个功能 不知怎么实现
v.invalidate();
return false;
}
});
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
Paint paint = new Paint(); // 笔触
paint.setAntiAlias(true); // 反锯齿
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
canvas.drawRect(x, y, w, h, paint);
Log.i("mian", "--------->>执行: x=" + x + " | y=" + y +" | w="+ w + " | h=" + h);
super.onDraw(canvas);
}
}
布局文件:
各位大神我想在view的基础上添加个类似于android系统在带的图片裁剪框,可以手动调整大小的那种,希望能给点思路!谢谢。