2012-12-06 31 views
3

我正在構建一個自動對焦的自定義相機,並且只是想知道是否有方法來調用本機相機所具有的相同自動對焦矩形指示器,或者如果需要構建從頭開始..任何示例或教程鏈接將不勝感激。正在尋找相機自動對焦指示器示例

+0

您是否找到解決方案?我面臨同樣的問題。請儘快通知我。 –

回答

11

看看最新的Jelly Bean 4.2相機處理這種情況的方式可能會有幫助。可以按如下方式下載相機來源:

git clone https://android.googlesource.com/platform/packages/apps/Camera.git 

一旦你的代碼,瀏覽到FocusOverlayManager類和PieRenderer類。如果您之前沒有試用過這個最新版本,焦點計是一個餅狀圓,可以在焦點完成時旋轉。您可以在photoshop中製作自己的廣場,或者使用以前使用過的兩種廣告素材中的一種(其中一種是我製作的iPhone ripoff,另一種是用於某些版本的安卓相機的九貼片):

enter image description hereenter image description here

的果凍豆例子可能是你在找什麼有點複雜,所以下面是我實現自動對焦的視覺反饋的方式的一些準則。這個過程可能有點複雜。我不假裝自己的方式是做到這一點的最佳方式,但下面是一些示例代碼,可以爲您提供一般想法......

在我的相機預覽佈局的xml文件:

<!-- Autofocus crosshairs --> 

<RelativeLayout 
    android:id="@+id/af_casing" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerInParent="true" 
    android:clipChildren="false" > 

    <com.package.AutofocusCrosshair 
     android:id="@+id/af_crosshair" 
     android:layout_width="65dp" 
     android:layout_height="65dp" 
     android:clipChildren="false" > 
    </com.package.AutofocusCrosshair> 
</RelativeLayout> 

這AutofocusCrosshair類如下:

public class AutofocusCrosshair extends View { 

    private Point mLocationPoint; 

    public AutofocusCrosshair(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    private void setDrawable(int resid) { 
     this.setBackgroundResource(resid); 
    } 

    public void showStart() { 
     setDrawable(R.drawable.focus_crosshair_image); 
    } 

    public void clear() { 
     setBackgroundDrawable(null); 
    } 

} 

當,在我的活動,我要開始自動對焦我做到以下幾點:

mAutofocusCrosshair = (AutofocusCrosshair) findViewById(R.id.af_crosshair); 
//Now add your own code to position this within the view however you choose 
mAutofocusCrosshair.showStart(); 
//I'm assuming you'll want to animate this... so start an animation here 
findViewById(R.id.af_casing).startAnimation(mAutofocusAnimation); 

並確保在動畫結束時清除圖像:

mAutofocusAnimation.setAnimationListener(new AnimationListener() { 
    @Override public void onAnimationEnd(Animation arg0) { 
     mAutofocusCrosshair.clear();    
    } 
    @Override public void onAnimationRepeat(Animation arg0) {} 
    @Override public void onAnimationStart(Animation arg0) {} 
}); 
+0

非常好的答案!讓我看起來積極的意思:-( – emrys57

+0

敬畏emrys57你是甜蜜的心哈哈..丹尼爾謝謝..但我有點困惑,動畫。部分..你如何動畫它與實際的重點 – erik

+0

那麼自動對焦本身是一個完全獨立的東西,它涉及到相機的調用,可以在http://developer.android.com/reference/android/hardware/Camera.html#autoFocus(android.hardware.Camera.AutoFocusCallback)找到它。動畫可以在自動對焦發生的時候發生,我把動畫留給了你,因爲這是個人的,並且在相機之間是不同的。謎題:) –

1

如果您的意思是在相機應用程序的預覽屏幕中改變顏色的小矩形,我很確定您必須自己繪製。對不起,如果這不是你想要的答案!

但是,您可以致電autoFocus(),它稍後會提供一個結果,以指示相機是否處於對焦狀態。由於API 14,即使攝像機位於FOCUS_MODE_CONTINUOUS_PICTURE,也能正常工作。

對不起,我也不知道描述使用焦點機制的好教程。在過去的一週中我學到了一件事:在啓動預覽圖像之前不要撥打autoFocus(),因爲它會使HTC Nexus One崩潰。

我從示例代碼http://marakana.com/forums/android/examples/39.html 構建了我的第一個Android相機應用程序,但請注意,其中寫入的代碼會將每個預覽幀寫入SD卡並快速填充它!那裏沒有關於自動對焦的代碼。

編輯:當然,最終的示例代碼(包括焦點指示符)位於相機應用源代碼中。這個問題:Where can I get the Android camera application source code?講述如何得到它。我只是遵循那裏的指令,得到了大約35M字節的源代碼,恐怕我還沒有找到那個小的聚焦矩形!