学习AsyncTask运行机制
mWorker设置优先级
将mWorker传给mFuture,调用call()->调用doInBackground()
三个参数;
1.doInBackground的参数类型
2.任务进度类型(任务运行进度百分比)
3.doInBackground的返回值类型,以及onPostExecute参数类型,doInBackground的返回值传给onPostExecute
阅读camera文档
使用api运行相机,
程序流程:
编写preview类,用于预览相机图像
继承SurfaceView类,重载surfaceCreated(将预览放入holder) surfaceChanged(打开相机,启动预览) surfaceDestroyed(释放相机)函数
拍照调用takePicture函数 第三个参数PictureCallback mPicture用于处理照片相关处理(储存)
预览类
public class MySurfaceView extends SurfaceView implements
SurfaceHolder.Callback {
private static final String TAG = "MySurfaceView";
private static SurfaceHolder holder;
private Camera mCamera;
public MySurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i(TAG, "new View ...");
holder = getHolder();//
holder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
Log.i(TAG, "surfaceCreated...");
if (mCamera == null) {
mCamera = Camera.open();//开启相机,可以放参数 0 或 1,分别代表前置、后置摄像头,默认为 0
try {
mCamera.setPreviewDisplay(holder);//整个程序的核心,相机预览的内容放在 holder
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
Log.i(TAG, "surfaceChanged...");
mCamera.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
Log.i(TAG, "surfaceChanged...");
if (mCamera != null) {
mCamera.release();//释放相机资源
mCamera = null;
}
}
public void mtakePicture(){
mCamera.takePicture(null,null,mPicture) ;
}
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
public static final int MEDIA_TYPE_IMAGE = 1;
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
camera.startPreview(); //拍照完成后,要继续打开预览,否则会卡住,因为拍照会停止预览
}
};
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = "test";
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
return mediaFile;
}
}
MainActivity 拍照不需要使用线程,在onClick函数中直接调用函数就好,就当作学习了private static final String TAG = "MainActivity"; private MySurfaceView mView; private Camera mCamera; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); Button button=(Button)findViewById(R.id.btn); mView = (MySurfaceView) findViewById(R.id.mView); button.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ Thread thread=new TakePicThread(mView); thread.start(); } }); } public class TakePicThread extends Thread { private MySurfaceView view; //继承Thread类,并改写其run方法 private final static String TAG = "My Thread ===> "; public TakePicThread(MySurfaceView v){ view=v; } public void run(){ Log.d(TAG, "run"); view.mtakePicture(); } }
代码摘自:http://blog.youkuaiyun.com/kintai/article/details/48597441
https://developer.android.google.cn/guide/topics/media/camera.html