行走的Coder

俱怀逸兴壮思飞,欲上青天览明月。

导航

(Java和C++)二进制date数据写进android保存为yuv格式

Java实现函数:

String strpath = "/storage/emulated/0/DCIM/" +  i + "output.yuv";

 boolean isInfiel= writeFile(strpath, frameData);

public boolean writeFile(String path, byte[] data) {
            FileOutputStream out = null;
            try {
                File file = new File(path);
                File parent = file.getParentFile();
                if (parent != null && !parent.exists())
                    parent.mkdirs();
                if (!file.exists()){
                    boolean isok    =  file.createNewFile();
                   out = new FileOutputStream(path);
                   out.write(data);
                   FileDescriptor fd = out.getFD();
                   fd.sync();
                return true;
                }
            } catch (Exception e) {
                e.printStackTrace();

            } finally {
                try {
                    if (out != null)
                        out.close();
                } catch (Exception e) {
                }
            }
            return false;
        }

C++实现函数:

static void dumpYuvToFile(const int width, const int height, void *yBuf, void *uvBuf ) {
    static int i = 0;
    char buf[256] = { '\0' };
    LOGE(" dumpYuvToFile(): i is %d", i);
    snprintf(buf, sizeof(buf), "/data/misc/media/_%d_%d_%d.yuv",  width, height, i);
    FILE* file_fd = fopen(buf, "wb");
    if (file_fd != NULL) {
        void *data = NULL;
        int written_len = 0;
        data = (void *) ((uint8_t *) yBuf);
        written_len += fwrite(data, width * height, 1, file_fd);
        if (NULL == uvBuf)
            data = (void *) ((uint8_t *) yBuf + width * height);
        else
            data = (void *) ((uint8_t *) uvBuf);
        written_len += fwrite(data, width * height / 2, 1, file_fd);
        fclose(file_fd);
        LOGE("%s: dumpYuvToFile(): -------sucess", __func__);
    } else {
        LOGE("%s:dumpYuvToFile(): fail t open file for image dumping", __func__);
    }
    i++;
}

posted on 2016-10-27 15:15  行走的coder  阅读(1532)  评论(0编辑  收藏  举报