標籤:des android blog http io ar os java for
//儲存一張照片String fileName = "IMG_" + String.valueOf(index) + ".jpg"; //jpeg檔案名稱定義File sdRoot = Environment.getExternalStorageDirectory(); //系統路徑String dir = "/jpeg/"; //檔案夾名File mkDir = new File(sdRoot, dir); if (!mkDir.exists()){ mkDir.mkdirs(); //目錄不存在,則建立}File pictureFile = new File(sdRoot, dir + fileName);if (!pictureFile.exists()) { try { pictureFile.createNewFile(); FileOutputStream filecon = new FileOutputStream(pictureFile); YuvImage image = new YuvImage(data, ImageFormat.NV21, width, height, null); //將NV21 data儲存成YuvImage //映像壓縮 image.compressToJpeg( new Rect(0, 0, image.getWidth(), image.getHeight()), 70, filecon); // 將NV21格式圖片,以品質70壓縮成Jpeg,並得到JPEG資料流 }catch (IOException e) { e.printStackTrace(); }}
該方法,常常在
Camera.PreviewCallback
中採用:
@Overridepublic void onPreviewFrame(byte[] data, Camera camera) {}
將NV21資料壓縮成JPEG,並得到JPEG byte資料,解壓JPEG byte資料成一張Bitmap
@Overridepublic void onPreviewFrame(byte[] bytes, Camera camera) { YuvImage image = new YuvImage(bytes, ImageFormat.NV21, width, height, null); //ImageFormat.NV21 640 480 ByteArrayOutputStream outputSteam = new ByteArrayOutputStream(); image.compressToJpeg(new Rect(0, 0, image.getWidth(), image.getHeight()), 70, outputSteam); // 將NV21格式圖片,以品質70壓縮成Jpeg,並得到JPEG資料流 byte[] jpegData = outputSteam.toByteArray(); //從outputSteam得到byte資料 Options options = new BitmapFactory.Options(); options.inSampleSize = 1; Bitmap bmp = BitmapFactory.decodeStream(jpegData, null, options);}
http://my.oschina.net/eclipse88/blog/80115
Android -- 將NV21映像儲存成JPEG