2014-06-26 36 views
1

我想將圖像濾鏡應用於我的圖像並使用android HelloEffects sample如何在Android中將opengl紋理轉換回位圖?

它將圖像數據從位圖轉換爲紋理。
在應用圖像濾鏡效果後,我想將圖像恢復爲jpeg格式,但不知道如何去做。

+0

這是怎麼翻出來的嗎?我也想使用媒體效果,但我需要能夠將結果保存到位圖。我開始問你同樣的問題,但後來發現了這個問題。是否將位圖從GL紋理保存回正確的方法? – startoftext

回答

1

我在單程像這樣做提到的saveFrame()方法,

  1. 我使用glreadpixles()方法

  2. 我轉換紋理圖像爲位圖然後將位圖保存到SD卡

紋理圖編碼

public static Bitmap SavePixels(int x, int y, int w, int h){ 
    int b[]=new int[w*(y+h)]; 
    int bt[]=new int[w*h]; 
    IntBuffer ib = IntBuffer.wrap(b); 
    ib.position(0); 
    GLES20.glReadPixels(0, 0, w, h, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ib); 

    for(int i=0, k=0; i<h; i++, k++) 
    {//remember, that OpenGL bitmap is incompatible with Android bitmap 
     //and so, some correction need. 
     for(int j=0; j<w; j++) 
     { 
      int pix=b[i*w+j]; 
      int pb=(pix>>16)&0xff; 
      int pr=(pix<<16)&0x00ff0000; 
      int pix1=(pix&0xff00ff00) | pr | pb; 
      bt[(h-k-1)*w+j]=pix1; 
     } 
    } 

    Bitmap sb=Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888); 
    return sb; 
} 

位圖的內部存儲代碼,

public static void saveImage(Bitmap finalBitmap) { 

    File myDir=new File("/sdcard/saved_images"); 
    myDir.mkdirs(); 
    Random generator = new Random(); 
    int n = 10000; 
    n = generator.nextInt(n); 
    String fname = "Image-"+ n +".jpg"; 
    File file = new File (myDir, fname); 
    if (file.exists()) file.delete(); 
    try { 
     FileOutputStream out = new FileOutputStream(file); 
     finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
     out.flush(); 
     out.close(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
}