android调用摄像头并拍照,Android调用系统自带的摄像头进行拍照

编程入门 行业动态 更新时间:2024-10-24 22:18:01

android调用<a href=https://www.elefans.com/category/jswz/34/1767894.html style=摄像头并拍照,Android调用系统自带的摄像头进行拍照"/>

android调用摄像头并拍照,Android调用系统自带的摄像头进行拍照

一:布局文件

xmlns:tools=""

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/openCameraPlay"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="拍照" />

android:id="@+id/showImageView"

android:layout_width="match_parent"

android:layout_height="match_parent" />

二:主要代码

package com.example.opeancamerademo;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import android.os.Bundle;

import android.provider.MediaStore;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

import android.app.Activity;

import android.content.Intent;

import android.graphics.Bitmap;

public class MainActivity extends Activity implements OnClickListener {

private Button openCameraPlay;

private ImageView showImageView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

openCameraPlay = (Button) findViewById(R.id.openCameraPlay);

showImageView = (ImageView) findViewById(R.id.showImageView);

openCameraPlay.setOnClickListener(this);

}

@Override

public void onClick(View view) {

// 1.这一种要设置权限才能启动(只要Action设置对了就能启动,有多种方式)

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(intent, 1);

// 2.这一中不要在Manifest中设置权限就能启动,这是调用系统自带的。

// Intent intent = new Intent();

// intent.setAction("android.media.action.STILL_IMAGE_CAMERA");

// startActivityForResult(intent, 1);

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (resultCode == Activity.RESULT_OK) {

Bundle bundle = data.getExtras();

// 获取相机返回的数据,并转换为Bitmap图片格式

Bitmap bitmap = (Bitmap) bundle.get("data");

FileOutputStream b = null;

File file = new File("/sdcard/testImage");

if (!file.exists())

file.mkdirs();

try {

b = new FileOutputStream("sdcard/testImage/test.jpg");

// 把数据写入文件

bitmappress(Bitmap.CompressFormat.JPEG, 100, b);

} catch (FileNotFoundException e) {

e.printStackTrace();

} finally {

try {

b.flush();

b.close();

b = null;

} catch (IOException e) {

e.printStackTrace();

}

}

showImageView.setImageBitmap(bitmap);

}

}

}

三:配置权限

更多推荐

android调用摄像头并拍照,Android调用系统自带的摄像头进行拍照

本文发布于:2024-02-12 03:58:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1685808.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:摄像头   系统自带   android   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!