将YV12(yuv420p)转换为JPEG / Bitamp

时间:2017-03-14 06:53:48

标签: android android-camera android-bitmap yuv

在使用onPreviewFrame的{​​{1}}中,如果我使用默认的android.hardware.Camera格式,我可以使用NV21压缩为jpeg格式,这非常有用。如果我尝试使用YuvImage更改格式,则它不再起作用,因为setPreviewFormat(ImageFormat.YV12)不支持YuvImage格式。我发现只有一个解决方案可以将YV12转换为Bitmap,但我想做相反的事情并从这些字节中获取jpeg。有没有图书馆可以做到这一点?

1 个答案:

答案 0 :(得分:0)

如果您正在寻找YUV420到JPEG,

public class chatAppGetter {

private String chatMessage;
private String author;
private String username;
private long chatTime;



public chatAppGetter(String chatMessage, String author) {
    this.chatMessage = chatMessage;
    this.author = author;

    chatTime = new Date().getTime();
}

public chatAppGetter() {
}

public String getChatMessage() {
    return chatMessage;
}

public void setChatMessage(String chatMessage) {
    this.chatMessage = chatMessage;
}

public long getChatTime() {
    return chatTime;
}

public void setChatTime(long chatTime) {
    this.chatTime = chatTime;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}
}


ListView messageList = (ListView)findViewById(R.id.listview);
    mAdapter = new FirebaseListAdapter<chatAppGetter>(this,    chatAppGetter.class, R.layout.chat_items, FirebaseDatabase.getInstance().getReference()) {
        @Override
        protected void populateView(View v, chatAppGetter model, int position) {

            TextView chatMessage, author, chatTime;
            chatMessage = (TextView)v.findViewById(R.id.chat_message);
            author = (TextView)v.findViewById(R.id.chat_user);
            chatTime = (TextView)v.findViewById(R.id.chat_time);

其中,decodeYUV420方法如下:

@Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        // TODO Auto-generated method stub
        ByteArrayOutputStream out= new ByteArrayOutputStream(); 
        decodeYUV420(pixels, data, previewSize.width, previewSize.height);
        mBitmap = Bitmap.createBitmap(pixels, previewSize.width, previewSize.height,Config.ARGB_8888);
        mBitmap.compress(CompressFormat.JPEG    , 25, out);
        .......

我在github check here

上创建了一个类似的项目

和代码实现:here

是的,它有效!