经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android实现简单的照相功能
来源:jb51  时间:2022/3/29 11:46:58  对本文有异议

一个简单的照相功能,拍照之后在另一个activit中显示出拍照的图片。
首先是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:app="http://schemas.android.com/apk/res-auto"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:orientation="vertical"
? ? tools:context=".MainActivity">

? ? <SurfaceView
? ? ? ? android:id="@+id/sf"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="0dp"
? ? ? ? android:layout_weight="1"
? ? ? ? android:text="Hello World!"
? ? ? ? app:layout_constraintBottom_toBottomOf="parent"
? ? ? ? app:layout_constraintLeft_toLeftOf="parent"
? ? ? ? app:layout_constraintRight_toRightOf="parent"
? ? ? ? app:layout_constraintTop_toTopOf="parent" />
? ? <Button
? ? ? ? android:id="@+id/bt"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:text="拍照"></Button>

</LinearLayout>

一个SurfaceView呈现相机拍摄的画面;
button是点击后拍照功能;

  • 初始化一个SurfaceView 控件;
sf = findViewById(R.id.sf);
? ? ? ? sf.getHolder().addCallback(new SurfaceHolder.Callback() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void surfaceCreated(SurfaceHolder holder) {
? ? ? ? ? ? ? ? start();
? ? ? ? ? ? }

? ? ? ? ? ? @Override
? ? ? ? ? ? public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

? ? ? ? ? ? }

? ? ? ? ? ? @Override
? ? ? ? ? ? public void surfaceDestroyed(SurfaceHolder holder) {
? ? ? ? ? ? ? ? stop();
? ? ? ? ? ? }
? ? ? ? }

简单说明一下Surface与SurfaceHolder.Callback之间的联系。

Surface是android的一个重要元素,用于android画面的图形绘制。而SurfaceView 是视图(View)的一个继承类,每一个SurfaceView都内嵌封装一个Surface。通过调用SurfaceHolder可以调用 SurfaceView,控制图形的尺寸和大小。而SurfaceHolder 是通过getholder()来取得。创立SurfaceHolder 对象后,用SurfaceHolder.Callback()来回调SurfaceHolder,对SurfaceView进行控制。

surfaceCreated 当Surface第一次创建后会立即调用该函数。程序可以在该函数中做些和绘制界面相关的初始化工作,一般情况下都是在另外的线程来绘制界面,所以不要在这个函数中绘制Surface。

surfaceChanged 当Surface的状态(大小和格式)发生变化的时候会调用该函数,在surfaceCreated调用后该函数至少会被调用一次。

surfaceDestroyed 当Surface被摧毁前会调用该函数,该函数被调用后就不能继续使用Surface了,一般在该函数中来清理使用的资源。

创建camera对象,(注意要用import android.hardware.Camera;这个包下的)

public void start() {
?camera = Camera.open();
? ? ? ? try {
? ? ? ? ? ? camera.setPreviewDisplay(sf.getHolder());
? ? ? ? ? ? camera.startPreview();//开始预览画面
? ? ? ? ? ? camera.setDisplayOrientation(90);//拍摄画面旋转90度
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }

把刚才创建的SurfaceHolder对象设置到camera中;
以上步骤在surfaceCreated()方法中调用;

在界面结束的时候释放相机资源:

public void stop() {
? ? ? ? camera.stopPreview();
? ? ? ? camera.release();
? ? }

点击拍照按钮之后执行的步骤

findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? camera.takePicture(null, null, new Camera.PictureCallback() {//开始拍照;
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onPictureTaken(byte[] data, Camera camera) {//拍完之后回调;
? ? ? ? ? ? ? ? ? ? ? ? String path = null;

? ? ? ? ? ? ? ? ? ? ? ? if ((path = savephoto(data)) != null) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? Intent in = new Intent(MainActivity.this, MyActivity.class);
? ? ? ? ? ? ? ? ? ? ? ? ? ? in.putExtra("path", path);
? ? ? ? ? ? ? ? ? ? ? ? ? ? startActivity(in);
? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this, "save photo fail", Toast.LENGTH_LONG).show();
? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? }
? ? ? ? });

savephoto()保存当前相片资源到临时文件中;

private String savephoto(byte[] bytes) {
? ? ? ? try {
? ? ? ? ? ? File f = File.createTempFile("img", "");//前缀,后缀
? ? ? ? ? ? FileOutputStream fos = new FileOutputStream(f);
? ? ? ? ? ? fos.write(bytes);
? ? ? ? ? ? fos.flush();
? ? ? ? ? ? fos.close();
? ? ? ? ? ? return f.getAbsolutePath();
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? return null;
? ? }

将二进制数据存储到临时文件中,并且返回文件路径;

拍照之后跳转到另个界面显示:

package com.example.camera;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import java.io.File;

public class MyActivity extends Activity {
? ? private ImageView iv;

? ? @Override
? ? protected void onCreate(@Nullable Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? iv=new ImageView(MyActivity.this);

? ? ? ? setContentView(iv);
? ? ? ? Intent intent =getIntent();
? ? ? ? String path=intent.getStringExtra("path");
? ? ? ? if (path!=null){
? ? ? ? ? ? iv.setImageURI(Uri.fromFile(new File(path)));
? ? ? ? }
? ? }
}

iv.setImageURI(Uri.fromFile(new File(path)));通过文件路径,创建一个文件;

主activity的代码如下:

package com.example.camera;

import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


public class MainActivity extends AppCompatActivity {
? ? private SurfaceView sf;
? ? private Camera camera;

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? camera.takePicture(null, null, new Camera.PictureCallback() {//开始拍照;
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onPictureTaken(byte[] data, Camera camera) {//拍完之后回调;
? ? ? ? ? ? ? ? ? ? ? ? String path = null;

? ? ? ? ? ? ? ? ? ? ? ? if ((path = savephoto(data)) != null) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? Intent in = new Intent(MainActivity.this, MyActivity.class);
? ? ? ? ? ? ? ? ? ? ? ? ? ? in.putExtra("path", path);
? ? ? ? ? ? ? ? ? ? ? ? ? ? startActivity(in);
? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this, "save photo fail", Toast.LENGTH_LONG).show();
? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? }
? ? ? ? });


? ? ? ? sf = findViewById(R.id.sf);
? ? ? ? sf.getHolder().addCallback(new SurfaceHolder.Callback() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void surfaceCreated(SurfaceHolder holder) {
? ? ? ? ? ? ? ? start();
? ? ? ? ? ? }

? ? ? ? ? ? @Override
? ? ? ? ? ? public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

? ? ? ? ? ? }

? ? ? ? ? ? @Override
? ? ? ? ? ? public void surfaceDestroyed(SurfaceHolder holder) {
? ? ? ? ? ? ? ? stop();
? ? ? ? ? ? }
? ? ? ? });

? ? }

? ? private String savephoto(byte[] bytes) {
? ? ? ? try {
? ? ? ? ? ? File f = File.createTempFile("img", "");//前缀,后缀
? ? ? ? ? ? FileOutputStream fos = new FileOutputStream(f);
? ? ? ? ? ? fos.write(bytes);
? ? ? ? ? ? fos.flush();
? ? ? ? ? ? fos.close();
? ? ? ? ? ? return f.getAbsolutePath();
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? return null;
? ? }

? ? public void start() {
? ? ? ? camera = Camera.open();
? ? ? ? try {
? ? ? ? ? ? camera.setPreviewDisplay(sf.getHolder());
? ? ? ? ? ? camera.startPreview();//开始预览画面
? ? ? ? ? ? camera.setDisplayOrientation(90);//拍摄画面旋转90度
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }

? ? public void stop() {
? ? ? ? camera.stopPreview();
? ? ? ? camera.release();
? ? }

}

记得需要添加照相机权限:

<uses-permission android:name="android.permission.CAMERA"></uses-permission>

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