当我按下按钮时,我制作了拍照的应用程序,当你打开app时我创建了另一个记录3秒视频的应用程序。我想在按下Capture按钮后立即开始录制视频。当我尝试在一个应用程序中连接捕获和录制时它总是崩溃。 如何连接这两个代码并使其在捕获图像后捕获视频?
这是按下按钮时拍摄和保存图片的代码
public class PreviewActivity extends Activity implements
SurfaceHolder.Callback, Camera.ShutterCallback, Camera.PictureCallback {
Camera mCamera;
SurfaceView mPreview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mPreview = (SurfaceView)findViewById(R.id.preview);
mPreview.getHolder().addCallback(this);
mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mCamera = Camera.open();
}
@Override
public void onPause() {
super.onPause();
mCamera.stopPreview();
}
@Override
public void onDestroy() {
super.onDestroy();
mCamera.release();
Log.d("CAMERA", "Destroy");
}
public void onCancelClick(View v) { // this is from Cancel Button
finish();
}
public void onSnapClick(View v) { // this is from Capture button
mCamera.takePicture(null, null, this);
}
@Override
public void onShutter() {
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
//Here, we chose internal storage
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
//write the file
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
camera.startPreview();
}
private static File getOutputMediaFile() {
//make a new file directory inside the "sdcard" folder
File mediaStorageDir = new File("/sdcard/", "Taken Picture");
//if this "JCGCamera folder does not exist
if (!mediaStorageDir.exists()) {
//if you cannot make this folder return
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
//take the current timeStamp
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
//and make a media file:
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
return mediaFile;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters=mCamera.getParameters();
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
mCamera.setDisplayOrientation(90);
parameters.set("rotation",90);
//parameters.set("orientation", "portrait");
Log.i("alengenije","portrait");
}
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
parameters.set("rotation", 0);
//parameters.set("orientation", "landscape");
Log.i("alengenije", "landscape");
}
mCamera.setParameters(parameters);
mCamera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(mPreview.getHolder());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i("PREVIEW","surfaceDestroyed");
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView
android:id="@+id/preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="100dip"
android:layout_alignParentBottom="true"
android:gravity="center_vertical"
android:background="#A000">
<Button
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="Cancel"
android:onClick="onCancelClick"
/>
<Button
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Snap Photo"
android:onClick="onSnapClick"
/>
</RelativeLayout>
</RelativeLayout>
这是录制视频3秒钟的代码:
public class VideoCapture extends SurfaceView implements SurfaceHolder.Callback {
private MediaRecorder recorder;
private SurfaceHolder holder;
public Context context;
private Camera camera;
public static String videoPath = Environment.getExternalStorageDirectory()
.getPath() +"/YOUR_VIDEO.mp4";
public VideoCapture(Context context) {
super(context);
this.context = context;
init();
}
public VideoCapture(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public VideoCapture(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
@SuppressLint("NewApi")
public void init() {
try {
recorder = new MediaRecorder();
recorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
Log.i("alengenije", "video END");
recorder.stop();
}
}
});
holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
camera = getCameraInstance();
camera.unlock();
recorder.setCamera(camera);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setMaxDuration(3000);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
recorder.setOutputFile(videoPath);
} catch (Exception e) {
e.printStackTrace();
}
}
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
public void surfaceCreated(SurfaceHolder mHolder) {
try {
recorder.setPreviewDisplay(mHolder.getSurface());
recorder.prepare();
recorder.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stopCapturingVideo() {
try {
recorder.stop();
camera.lock();
} catch (Exception e) {
e.printStackTrace();
}
}
@TargetApi(5)
public void surfaceDestroyed(SurfaceHolder arg0) {
if (recorder != null) {
stopCapturingVideo();
recorder.release();
camera.lock();
camera.release();
recorder = null;
}
}
private Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
} catch (Exception e) {
// Camera is not available (in use or does not exist)
}
return c;
}
}
答案 0 :(得分:0)
你可以开始录音4秒钟吗?然后从第一秒和&amp;休息是你的视频。这样您就不必两次启动相机。