Camera摄像头
摄像头 Camera
2023-09-14 09:00:42 时间
<LinearLayout
android:id="@+id/btn_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnClick1"
android:text="拍照1"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnClick2"
android:text="拍照2"/>
</LinearLayout>
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/btn_layout"/>
两种读取图片的方法
1.在拍照的时候传递一个文件地址,展示图片时即从该地址读取
2.拍照的时候将图片保存到数据库,展示图片时选取数据库最近的一张
public class MainActivity extends AppCompatActivity {
private ImageView iv;
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HH:mm:ss");
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = ((ImageView) findViewById(R.id.iv));
}
public void btnClick1(View view) {
Intent intent = new Intent();
//打开系统摄像头
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
//设置所拍照片的保存路径
file = new File(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Image-" + dateFormat.format(new Date()));
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, 1);
}
//data中携带的数据是所拍照片的缩略图
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//如果拍照成功
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
/***************************不添加图片保存路径时调用****************************/
//提取data中携带的Bitmap数据(如果在启动系统相机的时候已经指定了图片的保存位置,则这里不会返回缩略图)
// Bitmap bitmap = (Bitmap) data.getExtras().get("data");
// iv.setImageBitmap(bitmap);
// Log.d("lenve", "onActivityResult: width:" + bitmap.getWidth() + ";height:" + bitmap.getHeight());
/****************************添加图片保存路径后调用这里的方法,使用创建File的方式来构造一个Uri*****************************************/
// iv.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
/******************************使用ContentResolver来获得一个Uri**********************************************************************/
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media.DATA}
, null, null, MediaStore.Images.Media.DATE_ADDED + " desc");
String imageUrl = null;
if (cursor.moveToFirst()) {
imageUrl = cursor.getString(0);
iv.setImageBitmap(BitmapFactory.decodeFile(imageUrl));
}
cursor.close();
}
}
public void btnClick2(View view) {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
//设置图片名称
values.put(MediaStore.Images.Media.DISPLAY_NAME, "Image-" + dateFormat.format(new Date()) + ".png");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
//往图片数据库中存储数据
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, 1);
}
}
相关文章
- Qt音视频开发38-USB摄像头解码linux方案
- python 人脸识别(通过摄像头)
- 52. 如何使用 SAP UI5 的标准控件结合 Cordova 插件调用手机摄像头进行条形码扫描
- android v4l2-ctl工具测试验证Camera摄像头
- CV之OD:基于深度学习算法实现目标检测之GUI界面产品设计并实现图片识别、视频识别、摄像头识别(准确度非常高)
- android 11.0 12.0Camera旋转摄像头方向
- Android 10.0去掉前置摄像头闪光灯功能
- 树莓派usb摄像头用fswebcam 设置
- 树莓派发现不了摄像头
- (转)树莓派摄像头安装与配置
- 虚拟机使用pc摄像头
- jetson Nano 调试 CSI 接口的摄像头,并用 opencv 打开摄像头