2012-07-18 85 views

回答

56

如果你想使用帶紋理表面的相機,你可以實現SurfaceTextureListener接口。你必須實現4種方法:

1)onSurfaceTextureAvailable - 在這裏你設置你的相機

2)onSurfaceTextureSizeChanged - 在你的情況下,Android的相機會處理這個方法

3)onSurfaceTextureDestroyed - 在這裏你銷燬所有相機的東西。

4)onSurfaceTextureUpdated - 當你有東西要改變時更新你的紋理!

檢查下面的例子:

public class MainActivity extends Activity implements SurfaceTextureListener{ 

    private Camera mCamera; 
    private TextureView mTextureView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     mTextureView = new TextureView(this); 
     mTextureView.setSurfaceTextureListener(this); 

     setContentView(mTextureView); 
    } 

    @Override 
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { 
     mCamera = Camera.open(); 

     Camera.Size previewSize = mCamera.getParameters().getPreviewSize(); 
     mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
       previewSize.width, previewSize.height, Gravity.CENTER)); 

     try { 
      mCamera.setPreviewTexture(surface); 
     } catch (IOException t) { 
     } 

     mCamera.startPreview(); 

    } 

    @Override 
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { 
     // Ignored, the Camera does all the work for us 
    } 

    @Override 
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { 
     mCamera.stopPreview(); 
     mCamera.release(); 
     return true; 
    } 

    @Override 
    public void onSurfaceTextureUpdated(SurfaceTexture surface) { 
     // Update your view here! 
    } 
} 

兩件事:不要忘記添加到項目的清單攝影機權限和SurfaceTexture可從API 11

+4

不要忘記將相機權限添加到清單 – Gerard 2013-08-22 20:58:30

+0

請您引導我使用該服務? – someone 2014-07-17 18:56:24

+0

@Rhth,你使用這個服務是什麼意思? – 2014-07-18 12:46:15

-3
public class VideoCapture extends Activity implements OnClickListener, SurfaceHolder.Callback { 
MediaRecorder recorder; 
SurfaceHolder holder; 
boolean recording = false; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
requestWindowFeature(Window.FEATURE_NO_TITLE); 
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
     WindowManager.LayoutParams.FLAG_FULLSCREEN); 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

recorder = new MediaRecorder(); 
initRecorder(); 

SurfaceView cameraView = new SurfaceView(this); 
holder = cameraView.getHolder(); 
holder.addCallback(this); 
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

cameraView.setClickable(true); 
cameraView.setOnClickListener(this); 
} 

private void initRecorder() { 
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); 
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); 

CamcorderProfile cpHigh = CamcorderProfile 
     .get(CamcorderProfile.QUALITY_HIGH); 
recorder.setProfile(cpHigh); 
recorder.setOutputFile("/sdcard/videocapture_example.mp4"); 
recorder.setMaxDuration(50000); // 50 seconds 
recorder.setMaxFileSize(5000000); // Approximately 5 megabytes 
} 

private void prepareRecorder() { 
recorder.setPreviewDisplay(holder.getSurface()); 

try { 
    recorder.prepare(); 
} catch (IllegalStateException e) { 
    e.printStackTrace(); 
    finish(); 
} catch (IOException e) { 
    e.printStackTrace(); 
    finish(); 
} 
} 

public void onClick(View v) { 
if (recording) { 
    recorder.stop(); 
    recording = false; 

    // Let's initRecorder so we can record again 
    initRecorder(); 
    prepareRecorder(); 
} else { 
    recording = true; 
    recorder.start(); 
} 
} 

public void surfaceCreated(SurfaceHolder holder) { 
prepareRecorder(); 
} 

public void surfaceChanged(SurfaceHolder holder, int format, int width, 
    int height) { 
} 

public void surfaceDestroyed(SurfaceHolder holder) { 
if (recording) { 
    recorder.stop(); 
    recording = false; 
} 
recorder.release(); 
finish(); 
} 
} 
+1

謝謝您的回答但這不是我要找的。這個例子已經在api demo中可用。我想用SurfaceTexture做同樣的事情。 – AndroDev 2012-07-18 10:43:15