首页 > 其他分享 >Android开发学习之路--Camera之初体验

Android开发学习之路--Camera之初体验

时间:2023-01-15 16:08:27浏览次数:52  
标签:intent1 初体验 layout -- Camera Intent import android id


    顾名思义Camera就是拍照和录像的功能,像微信里面,我们想拍照传一下照片,就可以通过camera来拍照,然后存储照片,发送给好友。那么微信的app里面是不会直接通过camera api来实现的,因为系统一般都会有camera这个程序,那么直接调用camera app来实现拍照的功能不是很方便嘛,这里我们学习下。其实最终camera调用到android底层的是v4l2的接口,关于v4l2,还有android的camera的框架以后有机会再好好研究研究。

    调用系统自带的camera需要用到intent,通过MediaStore获取照片路径,下面来试一下,新建工程CameraPictureTest,为layout添加代码如下:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="10dp">

<ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/take_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拍照"/>

</LinearLayout>


    编写代码如下:


package com.example.jared.camerapicturetest;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

public static final int TAKE_PHOTO = 1;
public static final int CROP_PICTURE = 2;

private Button takePhoto;
private ImageView picture;
private Uri imageUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

takePhoto = (Button)findViewById(R.id.take_photo);
takePhoto.setOnClickListener(new myOnClickListener());

picture = (ImageView)findViewById(R.id.picture);
picture.setOnClickListener(new myOnClickListener());
}

private class myOnClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.take_photo:
setTakePhoto();
break;
default:
break;
}
}
}

public void setTakePhoto() {
File outputImage = new File(Environment.getExternalStorageDirectory(), "test.jpg");
try {
if(outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PHOTO);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PHOTO:
if(resultCode == RESULT_OK) {
Intent intent1 = new Intent("com.android.camera.action.CROP");
intent1.setDataAndType(imageUri, "image/*");
intent1.putExtra("scale", true);
intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent1, CROP_PICTURE);
}
break;
case CROP_PICTURE:
if(resultCode == RESULT_OK) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageUri));
picture.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
}


    这里首先确定了保存的路径为根目录下的test.jpg,然后通过intent,传入这个路径的Uri,打开相机进行拍照,这里有对拍照的返回,如果返回成功,那么就调用CROP的功能对照片进行裁剪,进入到裁减后返回成功就把图片显示在layout创建的ImageView中。

    具体需要真机显示,这里再插播一段关于真机屏幕在mac电脑上的显示,具体可以参考这篇文章,​​将你的安卓手机屏幕共享到PC或Mac上​​。通过一个chrome的Vysor插件来实现,需要android的5.0以上的版本才可以。

    好了,下面看下显示的效果:

Android开发学习之路--Camera之初体验_ide

  

Android开发学习之路--Camera之初体验_java_02

   

Android开发学习之路--Camera之初体验_android_03

    效果基本上出来了,很不错的插件。微信里面很多不是直接拍照发送的,还有通过选择相册的图片,已经拍好的照片来发送图片的,那么接着我们来实现这个功能,首先layout添加了choosephoto:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="10dp">

<ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/take_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拍照"/>

<Button
android:id="@+id/choose_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选取照片"/>
</LinearLayout>


    接着修改MainActivity代码如下:


package com.example.jared.camerapicturetest;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

public static final int TAKE_PHOTO = 1;
public static final int CROP_PICTURE = 2;

private Button takePhoto;
private Button choosePhoto;
private ImageView picture;
private Uri imageUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

takePhoto = (Button)findViewById(R.id.take_photo);
takePhoto.setOnClickListener(new myOnClickListener());

choosePhoto = (Button)findViewById(R.id.choose_photo);
choosePhoto.setOnClickListener(new myOnClickListener());

picture = (ImageView)findViewById(R.id.picture);
picture.setOnClickListener(new myOnClickListener());
}

private class myOnClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.take_photo:
setTakePhoto();
break;
case R.id.choose_photo:
setChoosePhoto();
default:
break;
}
}
}

public void setChoosePhoto() {
File outputImage1 = new File(Environment.getExternalStorageDirectory(), "test1.jpg");
try {
if(outputImage1.exists()) {
outputImage1.delete();
}
outputImage1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage1);
Intent intent1 = new Intent("android.intent.action.GET_CONTENT");
intent1.setType("image/*");
intent1.putExtra("crop", true);
intent1.putExtra("scale", true);
intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent1, CROP_PICTURE);

}

public void setTakePhoto() {
File outputImage = new File(Environment.getExternalStorageDirectory(), "test.jpg");
try {
if(outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PHOTO);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PHOTO:
if(resultCode == RESULT_OK) {
Intent intent1 = new Intent("com.android.camera.action.CROP");
intent1.setDataAndType(imageUri, "image/*");
intent1.putExtra("scale", true);
intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent1, CROP_PICTURE);
}
break;
case CROP_PICTURE:
if(resultCode == RESULT_OK) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageUri));
picture.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
}


    基本上和拍照也差不多,然后我们运行下看看效果:

Android开发学习之路--Camera之初体验_java_04

  

Android开发学习之路--Camera之初体验_ide_05

  

Android开发学习之路--Camera之初体验_java_06

     点击选择照片按钮,我们会进入到相册的app里面,然后选择一张照片,然后裁剪后保存,如上图所示。



附:参考《第一行代码》

标签:intent1,初体验,layout,--,Camera,Intent,import,android,id
From: https://blog.51cto.com/u_15940062/6008775

相关文章

  • Android开发学习之路--数据持久化之初体验
      上班第一天,虽然工作上处于酱油模式,但是学习上依旧不能拉下,接着学习android开发吧,这里学习数据持久化的知识。  其实数据持久化就是数据可以保存起来,一般我们保存......
  • Android开发学习之路--UI之初体验
      之前都是学习Activity,对于布局都没有做过学习,这里就简单学习下吧。下面看下AndroidStudio下有哪些控件:  这里分为Widgets,TextFields,Containers,Date&Time和Exp......
  • Android开发学习之路--Activity之Intent
      窗外再次飘起了小雪,还有1周就过年了,2016年即将到来,来年不知道自己将身处何处,船到桥头自然直吧。还是继续学习吧,上次学习了Activity,那么如果是两个Activity之间,怎么从......
  • ubuntu20.04设置开机自启动
    参考链接ubuntu20.04设置开机自启动 ​​ubuntu20.04设置开机自启动-GEGEWU--博客园(cnblogs.com)​​Docker部署后开机自启docker-compose容器参考链接​​Docker部......
  • Android开发学习之路--Android Studio项目目录结构简介
       既然已经搭建好环境了,那就对AndroidStudio中项目目录结构做个简单的了解了,这里以最简单的Hello工程为例子,新建好工程后看如下三个工程视图:1、Android工程manifest......
  • Android开发学习之路--Activity之初体验
      环境也搭建好了,android系统也基本了解了,那么接下来就可以开始学习android开发了,相信这么学下去肯定可以把android开发学习好的,再加上时而再温故下linux下的知识,看看an......
  • Android开发学习之路--MAC下Android Studio开发环境搭建
       一些资源可以参考如下网站,个人觉得不错:   资源提供网站: ​​http://www.androiddevtools.cn/​​   1、下载androidstudio。  下载好之后解压缩后,打......
  • 和菜鸟一起学linux之linux性能分析工具oprofile移植
    一、内核编译选项makemenuconfigGeneralsetup--->[*]Profilingsupport<*>OProfilesystemprofiling二、popt移植      下载源码:​​http://rpm5.org/files/p......
  • 和菜鸟一起学linux之upnp协议的学习记录
    UPnP全名是UniversalPlugandPlay,主要是微软在推行的一个标准。简单的来说,UPnP最大的愿景就是希望任何设备只要一接上网络,所有在网络上的设备马上就能知道有新设备加入,这......
  • 和菜鸟一起学linux之bluez学习记录1
    关于蓝牙协议栈体系结构 底层硬件模块 RF1、利用2400M~2483.5M频带2、采用调频方式传输数据,一共有79/EDR,40/BLE个hops,每秒3、采用GFSK(DQPSK和8DPSK)调制方式4、信道间隔(1......