经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android调用手机摄像头拍照和录音功能
来源:jb51  时间:2022/3/29 8:44:23  对本文有异议

本文实例为大家分享了Android调用手机摄像头拍照和录音功能的具体代码,供大家参考,具体内容如下

调用摄像头拍照:

  1. public class MainActivity extends Activity {
  2. ?
  3. ? ? private Button button;
  4. ? ? private ImageView imageView;
  5. ? ? @Override
  6. ? ? protected void onCreate(Bundle savedInstanceState) {
  7. ? ? ? ? super.onCreate(savedInstanceState);
  8. ? ? ? ? setContentView(R.layout.activity_main);
  9. ? ? ? ? imageView= (ImageView) findViewById(R.id.imageView);
  10. ? ? ? ? button= (Button) findViewById(R.id.btn);
  11. ? ? ? ? button.setOnClickListener(new View.OnClickListener() {
  12. ? ? ? ? ? ? @Override
  13. ? ? ? ? ? ? public void onClick(View view) {
  14. ? ? ? ? ? ? ? ? Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  15. ? ? ? ? ? ? ? ? startActivityForResult(intent,1);
  16. ? ? ? ? ? ? }
  17. ? ? ? ? });
  18. ? ? }
  19. ?
  20. ? ? @Override
  21. ? ? protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  22. ? ? ? ? super.onActivityResult(requestCode, resultCode, data);
  23. ? ? ? ? if(resultCode==RESULT_OK){
  24. ? ? ? ? ? ? Bundle bundle=data.getExtras();
  25. ? ? ? ? ? ? Bitmap bitmap= (Bitmap) bundle.get("data");
  26. ? ? ? ? ? ? if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
  27. ? ? ? ? ? ? ? ? File file=new File(Environment.getExternalStorageDirectory(),"MyImage");
  28. ? ? ? ? ? ? ? ? if(!file.exists()){
  29. ? ? ? ? ? ? ? ? ? ? file.mkdir();
  30. ? ? ? ? ? ? ? ? }
  31. ? ? ? ? ? ? ? ? try {
  32. ? ? ? ? ? ? ? ? ? ? String date=new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
  33. ? ? ? ? ? ? ? ? ? ? String path=file+"/"+date+".jpg";
  34. ? ? ? ? ? ? ? ? ? ? FileOutputStream outputStream=new FileOutputStream(path);
  35. ? ? ? ? ? ? ? ? ? ? bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
  36. ? ? ? ? ? ? ? ? } catch (FileNotFoundException e) {
  37. ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
  38. ? ? ? ? ? ? ? ? }
  39. ? ? ? ? ? ? }
  40. ? ? ? ? ? ? imageView.setImageBitmap(bitmap);
  41. ? ? ? ? }
  42. ? ? }
  43. }

布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. ? ? android:orientation="vertical"
  4. ? ? android:gravity="center"
  5. ? ? xmlns:tools="http://schemas.android.com/tools"
  6. ? ? android:layout_width="match_parent"
  7. ? ? android:layout_height="match_parent"
  8. >
  9. ?
  10. ? ? <Button
  11. ? ? ? ? android:id="@+id/btn"
  12. ? ? ? ? android:layout_width="wrap_content"
  13. ? ? ? ? android:layout_height="wrap_content"
  14. ? ? ? ? android:text="Hello World!" />
  15. ? ? <ImageView
  16. ? ? ? ? android:id="@+id/imageView"
  17. ? ? ? ? android:layout_width="200dp"
  18. ? ? ? ? android:layout_height="200dp" />
  19. </LinearLayout>

调用录音功能:

  1. public class Main2Activity extends Activity implements View.OnClickListener,AdapterView.OnItemClickListener{
  2. ?
  3. ? ? private ListView listView;//录音文件控件
  4. ? ? private Button btn1,btn2;//开始按钮和停止按钮
  5. ? ? private MediaRecorder recorder;//录音对象
  6. ? ? private List<String> list=new ArrayList<>();//录音文件数据源
  7. ? ? private File path,recorderFile;//根目录,要存入sd卡的录音文件
  8. ? ? private ArrayAdapter adapter;//适配器
  9. ? ? @Override
  10. ? ? protected void onCreate(Bundle savedInstanceState) {
  11. ? ? ? ? super.onCreate(savedInstanceState);
  12. ? ? ? ? setContentView(R.layout.activity_main2);
  13. ? ? ? ? init();
  14. ? ? ? ? if(null!=path){
  15. ? ? ? ? ? ? musicList();
  16. ? ? ? ? }
  17. ? ? }
  18. ?
  19. ? ? //初始化时获得所有录音文件
  20. ? ? private void musicList() {
  21. ? ? ? ? File home=path;
  22. ? ? ? ? //判断文件过滤器的长度是否大于0,大于则适配到listview上,小于则不设置上去
  23. ? ? ? ? if(home.listFiles(new MusicFilter()).length>0){
  24. ? ? ? ? ? ? for(File file:home.listFiles(new MusicFilter())){
  25. ? ? ? ? ? ? ? ? list.add(file.getName());
  26. ? ? ? ? ? ? }
  27. ? ? ? ? ? ? adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
  28. ? ? ? ? ? ? listView.setAdapter(adapter);
  29. ? ? ? ? }
  30. ? ? }
  31. ?
  32. ? ? private void init() {
  33. ? ? ? ? listView= (ListView) findViewById(R.id.listView);
  34. ? ? ? ? listView.setOnItemClickListener(this);
  35. ? ? ? ? btn1= (Button) findViewById(R.id.start);
  36. ? ? ? ? btn2= (Button) findViewById(R.id.stop);
  37. ? ? ? ? btn1.setOnClickListener(this);
  38. ? ? ? ? btn2.setOnClickListener(this);
  39. ? ? ? ? path=getPath();//获得根目录
  40. ? ? }
  41. ?
  42. ? ? private File getPath() {
  43. ? ? ? ? File file=null;
  44. ? ? ? ? //判断sd卡状态
  45. ? ? ? ? if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
  46. ? ? ? ? ? ? file=Environment.getExternalStorageDirectory();
  47. ? ? ? ? }else{
  48. ? ? ? ? ? ? Toast.makeText(this,"没有SD卡",Toast.LENGTH_SHORT).show();
  49. ? ? ? ? }
  50. ? ? ? ? return file;
  51. ? ? }
  52. ?
  53. ? ? @Override
  54. ? ? public void onClick(View view) {
  55. ? ? ? ? switch (view.getId()){
  56. ? ? ? ? ? ? //开始按钮
  57. ? ? ? ? ? ? case R.id.start:
  58. ? ? ? ? ? ? ? ? startRecorder();
  59. ? ? ? ? ? ? ? ? btn1.setEnabled(false);
  60. ? ? ? ? ? ? ? ? btn2.setEnabled(true);
  61. ? ? ? ? ? ? ? ? break;
  62. ? ? ? ? ? ? //停止按钮
  63. ? ? ? ? ? ? case R.id.stop:
  64. ? ? ? ? ? ? ? ? stopRecorder();
  65. ? ? ? ? ? ? ? ? btn1.setEnabled(true);
  66. ? ? ? ? ? ? ? ? btn2.setEnabled(false);
  67. ? ? ? ? ? ? ? ? break;
  68. ? ? ? ? }
  69. ? ? }
  70. ?
  71. ? ? private void stopRecorder() {
  72. ? ? ? ? //如果录音的文件不为null
  73. ? ? ? ? if(recorderFile!=null){
  74. ? ? ? ? ? ? //停止录音
  75. ? ? ? ? ? ? recorder.stop();
  76. ? ? ? ? ? ? //把录音文件的名字加入集合里
  77. ? ? ? ? ? ? list.add(recorderFile.getName());
  78. ? ? ? ? ? ? if(adapter!=null){
  79. ? ? ? ? ? ? ? ? //刷新适配器
  80. ? ? ? ? ? ? ? ? adapter.notifyDataSetChanged();
  81. ? ? ? ? ? ? }
  82. ? ? ? ? ? ? //释放录音对象
  83. ? ? ? ? ? ? recorder.release();
  84. ? ? ? ? ? ? recorder=null;
  85. ? ? ? ? }
  86. ?
  87. ? ? }
  88. ?
  89. ? ? private void startRecorder() {
  90. ? ? ? ? //创建录音对象
  91. ? ? ? ? recorder=new MediaRecorder();
  92. ? ? ? ? //设置麦克风
  93. ? ? ? ? recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  94. ? ? ? ? //设置转码类型
  95. ? ? ? ? recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  96. ? ? ? ? //设置编码方式
  97. ? ? ? ? recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  98. ? ? ? ? try {
  99. ? ? ? ? ? ? //创建录音文件
  100. ? ? ? ? ? ? recorderFile=File.createTempFile("录音_",".amr",path);
  101. ? ? ? ? ? ? //设置录音的数据写到录音文件里
  102. ? ? ? ? ? ? recorder.setOutputFile(recorderFile.getAbsolutePath());
  103. ? ? ? ? ? ? //录音准备
  104. ? ? ? ? ? ? recorder.prepare();
  105. ? ? ? ? ? ? //录音开始
  106. ? ? ? ? ? ? recorder.start();
  107. ? ? ? ? } catch (IOException e) {
  108. ? ? ? ? ? ? e.printStackTrace();
  109. ? ? ? ? }
  110. ? ? }
  111. ?
  112. ? ? @Override
  113. ? ? public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  114. ? ? ? ? //获得点击条目的路径
  115. ? ? ? ? ? ? File file=new File(path.getAbsolutePath()+File.separator+list.get(i));
  116. ? ? ? ? ? ? playMusic(file);
  117. ? ? }
  118. ?
  119. ? ? //调用播放器播放点击的条目文件
  120. ? ? private void playMusic(File file) {
  121. ? ? ? ? Intent intent = new Intent(Intent.ACTION_VIEW);
  122. ? ? ? ? Uri uri = Uri.fromFile(file);
  123. ? ? ? ? intent.setDataAndType(uri, "audio/mp3");
  124. ? ? ? ? startActivity(intent);
  125. ? ? }
  126. }

文件过滤代码:

  1. public class MusicFilter implements FilenameFilter {
  2. ? ? @Override
  3. ? ? public boolean accept(File file, String name) {
  4. ? ? ? ? return (name.endsWith(".amr"));
  5. ? ? }
  6. }

布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. ? ? android:orientation="vertical"
  4. ? ? xmlns:tools="http://schemas.android.com/tools"
  5. ? ? android:layout_width="match_parent"
  6. ? ? android:layout_height="match_parent">
  7. ?
  8. ? ? <Button
  9. ? ? ? ? android:id="@+id/start"
  10. ? ? ? ? android:text="开始录音"
  11. ? ? ? ? android:layout_width="match_parent"
  12. ? ? ? ? android:layout_height="wrap_content" />
  13. ? ? <Button
  14. ? ? ? ? android:id="@+id/stop"
  15. ? ? ? ? android:text="停止录音"
  16. ? ? ? ? android:layout_width="match_parent"
  17. ? ? ? ? android:layout_height="wrap_content" />
  18. ? ? <ListView
  19. ? ? ? ? android:id="@+id/listView"
  20. ? ? ? ? android:layout_width="match_parent"
  21. ? ? ? ? android:layout_height="match_parent"></ListView>
  22. ?
  23. </LinearLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号