2012-09-04 84 views
1

目前我正在處理像熱,單聲道等視頻效果。爲此,我使用預覽回調與畫布的東西。如何在Android上使用畫布保存視頻文件?

任何人都可以請告訴我如何保存該視頻?

下面我有我的回調代碼:

mCamera.setPreviewCallback(new PreviewCallback() { 
       public void onPreviewFrame(byte[] data1, Camera _camera) 
       { 
        Camera.Parameters parameters = _camera.getParameters(); 
        parameters.setColorEffect(Camera.Parameters.EFFECT_NONE); 
        previewSize = parameters.getPreviewSize(); 
        int frameSize = previewSize.width * previewSize.height; 
        int height = previewSize.height; 
        int width = previewSize.width; 
        int pixel; 
        int A, R, G, B; 
        final double GS_RED = 0.299; 
        final double GS_GREEN = 0.587; 
        final double GS_BLUE = 0.114; 
        int[] rgba = new int[frameSize+1]; 

        data = data1; 

         for (int i = 0; i < height; i++) 
          for(int j = 0; j < width; j++) 
          { 
           int y = (0xff & ((int) data[i * previewSize.width + j])); 
           int u = (0xff & ((int) data[frameSize + (i >> 1) * previewSize.width + (j & ~1) + 0])); 
           int v = (0xff & ((int) data[frameSize + (i >> 1) * previewSize.width + (j & ~1) + 1])); 

           y = y < 16 ? 16 : y; 

           int r = Math.round(1.164f * (y - 16) + 1.596f * (v - 128)); 
           int g = Math.round(1.164f * (y - 16) - 0.813f * (v - 128) - 0.391f * (u - 128)); 
           int b = Math.round(1.164f * (y - 16) + 2.018f * (u - 128)); 

           r = r < 0 ? 0 : (r > 255 ? 255 : r); 
           g = g < 0 ? 0 : (g > 255 ? 255 : g); 
           b = b < 0 ? 0 : (b > 255 ? 255 : b); 

           rgba[i * previewSize.width + j] = 0xff000000 + (b << 16) + (g<< 8) + r; 
         } 

         Bitmap bmp = Bitmap.createBitmap(width, height, 
         Bitmap.Config.RGB_565); 
         bmp.setPixels(rgba, 0 , width , 0, 0, width, height); 


        // scan through every single pixel 
         for(int x = 0; x < width; ++x) { 
          for(int y = 0; y < height; ++y) { 
           // get one pixel color 
           pixel = bmp.getPixel(x, y); 
           // retrieve color of all channels 
           A = Color.alpha(pixel); 
           R = Color.red(pixel); 
           G = Color.green(pixel); 
           B = Color.blue(pixel); 
           // take conversion up to one single value 
           R = G = B = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B); 
           // set new pixel color to output bitmap 
           bmp.setPixel(x, y, Color.argb(A, R, G, B)); 
          } 
         } 

         canvas = mHolder.lockCanvas(); 
         if (canvas != null) 
         { 
          canvas.drawBitmap(bmp, (canvas.getWidth() - width)/4, (canvas.getHeight() - height)/4, null); 
          mHolder.unlockCanvasAndPost(canvas); 
         } 
         bmp.recycle(); 
       } 
      }); 

回答

0

您可以保存每個幀,然後可以與ffmpeg的合併它們進行編碼,以創建視頻文件。

相關問題