2012-07-18 19 views

risposta

56

Se si desidera utilizzare la fotocamera con TextureSurface è possibile implementare SurfaceTextureListener interfaccia. Dovrete implementare 4 metodi:

1) onSurfaceTextureAvailable - Qui si imposta la fotocamera

2) onSurfaceTextureSizeChanged - Nel tuo caso, la fotocamera del Android gestirà questo metodo

3) onSurfaceTextureDestroyed - Qui distruggi tutte le cose della telecamera.

4) onSurfaceTextureUpdated - Aggiorna la tua texture qui quando hai qualcosa da cambiare!

Controllare l'esempio seguente:

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! 
    } 
} 

Altri due cose: Non dimenticare di aggiungere le autorizzazioni della fotocamera nel manifesto del progetto e la SurfaceTexture è disponibile da API 11.

+4

Non dimenticare di aggiungere l'autorizzazione Camera al manifest – Gerard

+0

Potresti per favore guidarmi sull'utilizzo di questo in servizio? – someone

+0

@Rhth, cosa intendi usando questo servizio? –

-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

Grazie per la risposta ma questo non è quello che sto cercando. Questo esempio è già disponibile nelle demo di api. Voglio fare la stessa cosa con SurfaceTexture. – AndroDev

Problemi correlati