2016-02-16 254 views
2

我試圖在我的相機應用程序中設置白平衡的自定義值&溫度顏色。我使用camera2 API和Im嘗試不同的方式來設置此值。我從excel文件中找到了一個方法,可以從100到100.000之間的白平衡值獲得正確的RGB溫度矩陣[紅,綠,藍]。Camera2 API設置自定義白平衡和溫度顏色

我將此方法附加到Seekbar並且它的工作正常,當我嘗試將某些白色物體聚焦時,出現問題,然後變爲粉紅色。屏幕上的任何一種燈光看起來都像粉紅色的火炬。 enter image description here

林以這種方式設定值:

mPreviewRequestBuilder.set(CaptureRequest.COLOR_CORRECTION_MODE, CaptureRequest.COLOR_CORRECTION_MODE_TRANSFORM_MATRIX); 

RggbChannelVector rggb = getTemperatureVector(seekBackSelectedTemperature); 

mPreviewRequestBuilder.set(CaptureRequest.COLOR_CORRECTION_GAINS, myRggbChannelVector); 

在其他的方式,我的方法獲取矩陣,這是一個:

public static RggbChannelVector getTemperatureVector (int WhiteBalanceValue){ 


    float InsertTemperature = WhiteBalanceValue; 
    float temperature = InsertTemperature/100; 
    float red; 
    float green; 
    float blue; 

    //Calculate red 

    if (temperature <= 66) 
     red = 255; 
    else { 
     red = temperature - 60; 
     red = (float) (329.698727446 * (Math.pow((double) red, -0.1332047592))); 
     if (red < 0) 
      red = 0; 
     if (red > 255) 
      red = 255; 
    } 


    //Calculate green 
    if (temperature <= 66) { 
     green = temperature; 
     green = (float) (99.4708025861 * Math.log(green) - 161.1195681661); 
     if (green < 0) 
      green = 0; 
     if (green > 255) 
      green = 255; 
    } else 
     green = temperature - 60; 
    green = (float) (288.1221695283 * (Math.pow((double) red, -0.0755148492))); 
    if (green < 0) 
     green = 0; 
    if (green > 255) 
     green = 255; 


    //calculate blue 
    if (temperature >= 66) 
     blue = 255; 
    else if (temperature <= 19) 
     blue = 0; 
    else { 
     blue = temperature - 10; 
     blue = (float) (138.5177312231 * Math.log(blue) - 305.0447927307); 
     if (blue < 0) 
      blue = 0; 
     if (blue > 255) 
      blue = 255; 
    } 
    RggbChannelVector finalTemperatureValue = new RggbChannelVector(red/255,(green/255)/2,(green/255)/2,blue/255); 
    return finalTemperatureValue; 
} 

也許其原因我CaptureRequest的方法是不正確的,但我沒有找到解決的辦法。希望有人對此有所瞭解!

非常感謝您提前。

回答

8

它改變後工作的模板Still_capture或手冊模板,並使用下一個方法:

captureBuilder.set(CaptureRequest.CONTROL_AWB_MODE, CaptureRequest.CONTROL_AWB_MODE_OFF); 
     // adjust color correction using seekbar's params 
     captureBuilder.set(CaptureRequest.COLOR_CORRECTION_MODE, CaptureRequest.COLOR_CORRECTION_MODE_TRANSFORM_MATRIX); 
     captureBuilder.set(CaptureRequest.COLOR_CORRECTION_GAINS, CameraCapabilities.colorTemperature(Integer.parseInt(awbMode))); 

public static RggbChannelVector colorTemperature(int whiteBalance) { 
    float temperature = whiteBalance/100; 
    float red; 
    float green; 
    float blue; 

    //Calculate red 
    if (temperature <= 66) 
     red = 255; 
    else { 
     red = temperature - 60; 
     red = (float) (329.698727446 * (Math.pow((double) red, -0.1332047592))); 
     if (red < 0) 
      red = 0; 
     if (red > 255) 
      red = 255; 
    } 


    //Calculate green 
    if (temperature <= 66) { 
     green = temperature; 
     green = (float) (99.4708025861 * Math.log(green) - 161.1195681661); 
     if (green < 0) 
      green = 0; 
     if (green > 255) 
      green = 255; 
    } else { 
     green = temperature - 60; 
     green = (float) (288.1221695283 * (Math.pow((double) green, -0.0755148492))); 
     if (green < 0) 
      green = 0; 
     if (green > 255) 
      green = 255; 
    } 

    //calculate blue 
    if (temperature >= 66) 
     blue = 255; 
    else if (temperature <= 19) 
     blue = 0; 
    else { 
     blue = temperature - 10; 
     blue = (float) (138.5177312231 * Math.log(blue) - 305.0447927307); 
     if (blue < 0) 
      blue = 0; 
     if (blue > 255) 
      blue = 255; 
    } 

    Log.v(TAG, "red=" + red + ", green=" + green + ", blue=" + blue); 
    return new RggbChannelVector((red/255) * 2, (green/255), (green/255), (blue/255) * 2); 
} 
+0

非常感謝你分享你的代碼。我正在尋找手動調整WB,你的代碼節省了我幾個小時。 – zeeshan

+1

讓輕鬆的隊友!這是一個痛苦的屁股在攝影論壇中找到這個將它添加到Android的。希望它能幫助你很多;) –

+0

我想用camera2 API,opencv的很多功能都照顧到了 –