public void onPreviewFrame(byte[] data, Camera camera) Size size = camera.getParameters().getPreviewSize(); try { YuvImage image = new YuvImage(data, ImageFormat.NV21,size.width, size.height, null); if (image != null) { // 保存图片 /// File file = new File(Environment.getExternalStorageDirectory().getPath() + File.separator+"picture.jpg"); FileOutputStream stream = new FileOutputStream(file); if (image.compressToJpeg(new Rect(0, 0, size.width, size.height), 80, stream)) { stream.flush(); stream.close(); / 转为Bitmap / ByteArrayOutputStream stream = new ByteArrayOutputStream(); image.compressToJpeg(new Rect(0, 0, size.width, size.height), 80, stream); Bitmap bmp=BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size()); stream.close(); } catch (Exception ex) { e.printStackTrace(); 写这篇文章前先调侃下Google的 Android 工程师们吧,作为一个普通的程序员,怎么都不敢去怀疑他们吧。直到现在我找到了标题中的问题,并且找到了解决方案,我依然认为这不是失误,应该是他们别有用意把! 在给Google提交了issue和解决方案,但是 一直没有人鸟我。 我认为 Android Framework的每一个API都应该是经过压测的,更别说是google这样一家有深度的公司,可惜并没有... 这个日志拖了很久了,是我的毕业设计中碰到的一个需求。 首先视频硬编码MediaRecorder类并不适合做实时发送(不知道为什么博主的机器测试出来的mdat后面紧接着并不是网上所说的一个Int表示的场长度,而是连续8个3F预留字节位,努力很久后彻底放弃了MediaRecorder,改为实现Camera的onPreviewFrame方法) Camera的onPreviewFrame会在每... @ volodymyr-kulyk提供的解决方案没有考虑图像中平面的行间距.下面的代码诀窍(图像是 android .media.Image类型):data = NV21toJPEG( YUV 420toNV21(image), image.getWidth(), image.getHeight(), 100);实施:private static byte[] NV21toJPEG(byte[] nv21... //格式成 YUV 格式 val yuv image = Yuv Image(data, ImageFormat.NV21, frame.size.width, frame.size.height, null) val baos = ByteArrayOutputStream() public static void save YUV toPicture(byte[] data,int width,int height) throws IOException{ FileOutputStream outStream = null; File file = new File("/mnt/sdcard/Camera"); if(!file.exists()){ 一首先将 YUV 的byte[] 保存 为 . yuv 后缀文件。 文件 保存 的路径是手机外部应用私有存储目录下面 private void saveAsJPEG(byte[] bytes, String path) { File file = new File(mActivity.getExternalFilesDir(null).getPath() + File.separator + System.currentTimeMillis() + path + "_. YUV "); // 保存 一张照片 String fileName = "IMG_" + String.valueOf(index) + ". jpg "; //jpeg文件名定义 File sdRoot = Environment.getExternalStorageDirectory(); //系统路径 String dir = "/jpeg/"; //文件夹名 File mkDir = new Fil