如何在android中使用相機,以便用戶可以在相同的相機應用程序中在相機模式和記錄器模式之間切換,因爲您可以在下方圖像的右側中央看到小滑塊控件。如何在android中使用攝像頭捕捉圖像和視頻?
回答
股票相機應用是開源的。爲什麼不只是take a look它是如何實現的?
我不知道它的工作或沒有,但嘗試通過在surfaceCreated(SurfaceHolder holder)
使用 camera.setPreviewDisplay(holder);
或recorder.setPreviewDisplay(holder);
這
你應該有 MediaRecorder recorder; Camera camera;
嘗試視頻和照片之間的切換。當你切換時,你應該重新創建surfaceView。股票相機應用重新創建整個活動。
添加切換按鈕並在其中設置一些值。讓我們說0或1 ,然後捕獲圖像/錄製視頻按鈕。 當切換值爲0時調用攝像機方法,當切換值爲1時調用視頻方法。
這裏是攝像頭和視頻錄製
實現一類兩類方法的代碼。然後調用onSnapClick攝像頭和startRecording()視頻。
希望這會有所幫助。
記錄視頻鏈接已損壞。 –
嘗試使用默認功能,攝像頭和視頻的喜歡這 -
Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String fileName = "KeyICamImage"+System.currentTimeMillis()+".JPG";
String mPathImage = Environment.getExternalStorageDirectory()+ "/" + fileName;
file = new File(mPathImage);
mImageCaptureUri= Uri.fromFile(file);
try
{
intent1.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,mImageCaptureUri);
startActivityForResult(intent1, PICK_FROM_CAMERA);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
使用'ACTION_IMAGE_CAPTURE'我們只能捕捉圖像,但不能錄製視頻。 – Chip
使用拖車按鈕一個用於圖像和其他在UI視頻....
/**** Camera/Video Demo ****************/
public class CameraDemoActivity extends Activity {
// Activity request codes
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
// directory name to store captured images and videos
private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";
private Uri fileUri; // file url to store image/video
private ImageView imgPreview;
private VideoView videoPreview;
private Button btnCapturePicture, btnRecordVideo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgPreview = (ImageView) findViewById(R.id.imgPreview);
videoPreview = (VideoView) findViewById(R.id.videoPreview);
btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
btnRecordVideo = (Button) findViewById(R.id.btnRecordVideo);
/*
* Capture image button click event
*/
btnCapturePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// capture picture
captureImage();
}
});
/*
* Record video button click event
*/
btnRecordVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// record video
recordVideo();
}
});
// Checking camera availability
if (!isDeviceSupportCamera()) {
Toast.makeText(getApplicationContext(),
"Sorry! Your device doesn't support camera",
Toast.LENGTH_LONG).show();
// will close the app if the device does't have camera
finish();
}
}
/**
* Checking device has camera hardware or not
* */
private boolean isDeviceSupportCamera() {
if (getApplicationContext().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
/*
* Capturing Camera Image will lauch camera app requrest image capture
*/
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
/*
* Here we store the file url as it will be
* null after returning from camera
* app
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save file url in bundle as it will be null on scren orientation
// changes
outState.putParcelable("file_uri", fileUri);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// get the file url
fileUri = savedInstanceState.getParcelable("file_uri");
}
/*
* Recording video
*/
private void recordVideo() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
// set video quality
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the
// image file
// name
// start the video capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
}
/**
* Receiving activity result method
* will be called after closing the camera
* */
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// successfully captured the image
// display it in image view
previewCapturedImage();
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
} else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// video successfully recorded
// preview the recorded video
previewVideo();
} else if (resultCode == RESULT_CANCELED) {
// user cancelled recording
Toast.makeText(getApplicationContext(),
"User cancelled video recording", Toast.LENGTH_SHORT)
.show();
} else {
// failed to record video
Toast.makeText(getApplicationContext(),
"Sorry! Failed to record video", Toast.LENGTH_SHORT)
.show();
}
}
}
/*
* Display image from a path to ImageView
*/
private void previewCapturedImage() {
try {
// hide video preview
videoPreview.setVisibility(View.GONE);
imgPreview.setVisibility(View.VISIBLE);
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
options);
imgPreview.setImageBitmap(bitmap);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
/*
* Previewing recorded video
*/
private void previewVideo() {
try {
// hide image preview
imgPreview.setVisibility(View.GONE);
videoPreview.setVisibility(View.VISIBLE);
videoPreview.setVideoPath(fileUri.getPath());
// start playing
videoPreview.pause();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* ------------ Helper Methods ----------------------
* */
/*
* Creating file uri to store image/video
*/
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/*
* returning image/video
*/
private static File getOutputMediaFile(int type) {
// External sdcard
// location.getExternalStoragePublicDirectory(
// Environment.DIRECTORY_PICTURES),IMAGE_DIRECTORY_NAME);
File mediaStorageDir = new File(
Environment
.getExternalStorageDirectory(),
IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else if (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "VID_" + timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
}
- 1. 捕捉攝像頭圖像,使用C#的視頻
- 2. 從iOS攝像頭捕捉視頻
- 3. 使用html5從網絡攝像頭捕捉音頻和視頻
- 4. 使用gstreamer從android攝像頭捕捉視頻
- 5. 如何在攝像頭閃光時使用圖像捕捉?
- 6. 如何使用c#在windows phone中使用前置攝像頭捕捉視頻#
- 7. 如何捕捉在asp.net網絡攝像頭視頻,C#
- 8. Ruby on Rails捕捉攝像頭視頻和音頻
- 9. 從網絡攝像頭本地捕捉視頻和音頻?
- 10. 使用vb.net攝像頭錄製視頻和捕獲圖像
- 11. 從網絡攝像頭捕捉圖像
- 12. java捕捉攝像頭圖像macbook
- 13. 從網絡攝像頭捕捉圖像
- 14. 使用用戶攝像頭使用GWT捕獲視頻/圖像
- 15. 如何從Android攝像頭捕捉圖像?
- 16. 前攝像頭視頻捕捉失真 - Android
- 17. 如何捕捉Python中的視頻(和音頻),從相機(或攝像頭)
- 18. 如何在python2中從網絡攝像頭捕捉圖像?
- 19. 使用QCView和iSight攝像頭捕捉到的圖像
- 20. 有沒有辦法在HTML5中捕捉攝像頭的視頻?
- 21. 網絡攝像頭捕捉
- 22. 慢速攝像頭捕捉
- 23. 爲什麼在Android上,OpenCV攝像頭在捕捉視頻時比Android攝像頭更快
- 24. JAVACV:使用javacv捕捉攝像頭
- 25. 使用圖片框從網絡攝像頭捕捉圖像
- 26. 我想用jQuery攝像頭插件捕捉視頻
- 27. Directshow如何使用Monogram X264編碼器從網絡攝像頭捕捉視頻?
- 28. 如何使用labview 2011從我的攝像頭捕捉實時視頻?
- 29. 如何在Android中通過攝像頭捕捉多張照片
- 30. ASP.net - 使用攝像頭捕捉視頻並在網頁中播放
請選中相應的問題作爲其他用戶的「最佳答案」。 – Darpan