2009-06-04 4 views
7

카메라 미리보기 용 맞춤 위젯을 개발하려고합니다. 카메라 미리보기로 화면을 채우고 그 위에 몇 개의 단추를 그립니다.Android : 카메라 미리보기 위젯을 만드는 방법?

은 내가 정의 다음과 같은 방법으로 위젯을 만들려고 한 :

import java.io.IOException; 

import android.content.Context; 
import android.hardware.Camera; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

class CapturePreview extends SurfaceView implements SurfaceHolder.Callback { 
    SurfaceHolder mHolder; 
    Camera mCamera; 

CapturePreview(Context context) { 
    super(context); 
    // Install a SurfaceHolder.Callback so we get notified when the 
    // underlying surface is created and destroyed. 
    mHolder = getHolder(); 
    mHolder.addCallback(this); 
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
} 

public void surfaceCreated(SurfaceHolder holder) { 
    // The Surface has been created, acquire the camera and tell it where 
    // to draw. 
    mCamera = Camera.open(); 
    try { 
     mCamera.setPreviewDisplay(holder); 
    } catch (IOException exception) { 
     mCamera.release(); 
     mCamera = null; 
     // TODO: add more exception handling logic here 
    } 
} 

public void surfaceDestroyed(SurfaceHolder holder) { 
    // Surface will be destroyed when we return, so stop the preview. 
    // Because the CameraDevice object is not a shared resource, it's very 
    // important to release it when the activity is paused. 
    mCamera.stopPreview(); 
    mCamera = null; 
} 

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
    // Now that the size is known, set up the camera parameters and begin 
    // the preview. 
    Camera.Parameters parameters = mCamera.getParameters(); 
    parameters.setPreviewSize(w, h); 
    mCamera.setParameters(parameters); 
    mCamera.startPreview(); 
} 

} 

을하고 main.xml에 파일을 다음과 같이 수정 : 액티비티 클래스에서

<RelativeLayout android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/RelativeLayout"> 
<dev.video.client.CapturePreview android:id="@+id/capturePreview" android:layout_width="wrap_content" android:layout_height="wrap_content"/> 
</RelativeLayout> 

내가 바인딩에 시도를 위젯 다음과 같은 방법 :

private CapturePreview mPreview; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setFullscreen(); 

    setContentView(R.layout.main); 

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

    //mPreview = new CapturePreview(this); 
    //setContentView(mPreview); 

    mPreview = (CapturePreview)this.findViewById(R.id.capturePreview); 
} 

내가 응용 프로그램을 디버깅 할 때마다 오류 메시지와 나는 무엇이 잘못 될 수 있는지 알지 못했습니다.

이 문제와 관련하여 도움을 주시면 감사하겠습니다.

감사합니다.

답변

4

당신은이 작업을 수행하려면 서피스 뷰 SurfaceView에 다음 생성자를 작성해야합니다 : 당신이 알 수 있듯이이 슈퍼 클래스의에서

CapturePreview(Context context, AttributeSet attrs)

, 자바 생성자는 상속되지 않습니다, 아직 생성자입니다 안드로이드 프로그래밍 방식으로 GUI를 정의하는 것과 달리 XML 파일에서 해당 구성 요소를 사용할 때 호출하려고합니다. 그게 당연히 작동하지 않을 것이므로, 제 의견으로는 훨씬 더 명확해야 할 오류 메시지입니다.

AttributeSet에는 XML 선언에 언급 된 모든 특성이 포함되어 있으므로 구성 요소에 대한 사용자 지정 특성이 있으면 생성자에서 해당 특성을 검색하고 그에 대한 작업을 수행 할 수 있습니다.

+0

안녕하세요, 저는 같은 질문을 가지고 있습니다.하지만 저는 ... –

-1

존경받는보기로 onTouchListener()를 구현할 수 있습니다. 언젠가 표면을 자동으로 클릭하면 Android System에서 onTouchListener() 메소드로 이동합니다.

관련 문제