安卓学习历程-第四天

学习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.csdn.net/kintai/article/details/48597441

                https://developer.android.google.cn/guide/topics/media/camera.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值