首页 > 移动平台 > 详细

Android Camera.PictureCallback 使用java nio提高生成照片(写文件)速度

时间:2016-02-26 12:11:54      阅读:504      评论:0      收藏:0      [点我收藏+]
 1     private Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
 2         public void onPictureTaken(byte[] data, Camera camera) {
 3             LogUtil.d("onPictureTaken - jpeg");
 4 //            new SaveImageTask().execute(data);
 5             saveImage(data);
 6         }
 7     };
 8 
 9     /**
10      * 使用java nio写照片 快
11      *
12      * @param data 照片元数据
13      */
14     private void saveImage(byte[] data) {
15         long time1 = System.currentTimeMillis();
16         String imgFileName = "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()) + ".jpg";
17         File imgFile = new File(mStoragePath + File.separator + imgFileName);
18 
19         FileOutputStream outputStream = null;
20         FileChannel fileChannel = null;
21         ByteBuffer buffer = null;
22         try {
23             outputStream = new FileOutputStream(imgFile);
24             fileChannel = outputStream.getChannel();
25             buffer = ByteBuffer.allocate(data.length);
26             buffer.put(data);
27             buffer.flip();
28             fileChannel.write(buffer);
29         } catch (IOException e) {
30             LogUtil.e(e.getMessage(), e);
31         } finally {
32             try {
33                 if (outputStream != null)
34                     outputStream.close();
35                 if (fileChannel != null)
36                     fileChannel.close();
37                 if (buffer != null) {
38                     buffer.clear();
39                 }
40             } catch (IOException e) {
41                 LogUtil.e(e.getMessage(), e);
42             }
43         }
44         long time2 = System.currentTimeMillis();
45         LogUtil.d("Write photo file took: " + (time2 - time1) + "ms.");
46 
47         mHandler.obtainMessage(MSG_UPDATE_PIC, imgFile.getAbsolutePath()).sendToTarget();
48 
49         startPreview();//重新开始预览
50         imgTakePhoto.setEnabled(true);//解除禁用
51     }
52 
53     // 使用java io接口写照片 慢
54     private class SaveImageTask extends AsyncTask<byte[], Void, String> {
55 
56         @Override
57         protected String doInBackground(byte[]... data) {
58             LogUtil.d("SaveImageTask doInBackground");
59 
60             long time1 = System.currentTimeMillis();
61             String imgFileName = "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()) + ".jpg";
62             File imgFile = new File(mStoragePath, imgFileName);
63 
64             FileOutputStream outputStream = null;
65             try {
66                 outputStream = new FileOutputStream(imgFile);
67                 outputStream.write(data[0]);
68                 outputStream.flush();
69                 outputStream.close();
70             } catch (IOException | NullPointerException e) {
71                 LogUtil.e(e.getMessage(), e);
72             } finally {
73                 try {
74                     if (outputStream != null)
75                         outputStream.close();
76                 } catch (IOException e) {
77                     LogUtil.e(e.getMessage(), e);
78                 }
79             }
80             long time2 = System.currentTimeMillis();
81             LogUtil.d("Write photo file took: " + (time2 - time1) + "ms.");
82             return null;
83         }
84 
85         @Override
86         protected void onPostExecute(String imgFilePath) {
87             super.onPostExecute(imgFilePath);
88             LogUtil.d("SaveImageTask onPostExecute");
89             mHandler.obtainMessage(MSG_UPDATE_PIC, imgFilePath).sendToTarget();
90 
91             startPreview();//重新开始预览
92             imgTakePhoto.setEnabled(true);//解除禁用
93         }
94     }

 

Android Camera.PictureCallback 使用java nio提高生成照片(写文件)速度

原文:http://www.cnblogs.com/yuanmingming/p/5219539.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!