欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)簡(jiǎn)單的照相功能

 更新時(shí)間:2022年03月29日 10:42:42   作者:安了個(gè)卓  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單的照相功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一個(gè)簡(jiǎn)單的照相功能,拍照之后在另一個(gè)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>

一個(gè)SurfaceView呈現(xiàn)相機(jī)拍攝的畫面;
button是點(diǎn)擊后拍照功能;

  • 初始化一個(gè)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();
? ? ? ? ? ? }
? ? ? ? }

簡(jiǎn)單說明一下Surface與SurfaceHolder.Callback之間的聯(lián)系。

Surface是android的一個(gè)重要元素,用于android畫面的圖形繪制。而SurfaceView 是視圖(View)的一個(gè)繼承類,每一個(gè)SurfaceView都內(nèi)嵌封裝一個(gè)Surface。通過調(diào)用SurfaceHolder可以調(diào)用 SurfaceView,控制圖形的尺寸和大小。而SurfaceHolder 是通過getholder()來取得。創(chuàng)立SurfaceHolder 對(duì)象后,用SurfaceHolder.Callback()來回調(diào)SurfaceHolder,對(duì)SurfaceView進(jìn)行控制。

surfaceCreated 當(dāng)Surface第一次創(chuàng)建后會(huì)立即調(diào)用該函數(shù)。程序可以在該函數(shù)中做些和繪制界面相關(guān)的初始化工作,一般情況下都是在另外的線程來繪制界面,所以不要在這個(gè)函數(shù)中繪制Surface。

surfaceChanged 當(dāng)Surface的狀態(tài)(大小和格式)發(fā)生變化的時(shí)候會(huì)調(diào)用該函數(shù),在surfaceCreated調(diào)用后該函數(shù)至少會(huì)被調(diào)用一次。

surfaceDestroyed 當(dāng)Surface被摧毀前會(huì)調(diào)用該函數(shù),該函數(shù)被調(diào)用后就不能繼續(xù)使用Surface了,一般在該函數(shù)中來清理使用的資源。

創(chuàng)建camera對(duì)象,(注意要用import android.hardware.Camera;這個(gè)包下的)

public void start() {
?camera = Camera.open();
? ? ? ? try {
? ? ? ? ? ? camera.setPreviewDisplay(sf.getHolder());
? ? ? ? ? ? camera.startPreview();//開始預(yù)覽畫面
? ? ? ? ? ? camera.setDisplayOrientation(90);//拍攝畫面旋轉(zhuǎn)90度
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }

把剛才創(chuàng)建的SurfaceHolder對(duì)象設(shè)置到camera中;
以上步驟在surfaceCreated()方法中調(diào)用;

在界面結(jié)束的時(shí)候釋放相機(jī)資源:

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

點(diǎn)擊拍照按鈕之后執(zhí)行的步驟

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) {//拍完之后回調(diào);
? ? ? ? ? ? ? ? ? ? ? ? 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()保存當(dāng)前相片資源到臨時(shí)文件中;

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;
? ? }

將二進(jìn)制數(shù)據(jù)存儲(chǔ)到臨時(shí)文件中,并且返回文件路徑;

拍照之后跳轉(zhuǎn)到另個(gè)界面顯示:

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)));通過文件路徑,創(chuàng)建一個(gè)文件;

主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) {//拍完之后回調(diào);
? ? ? ? ? ? ? ? ? ? ? ? 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();//開始預(yù)覽畫面
? ? ? ? ? ? camera.setDisplayOrientation(90);//拍攝畫面旋轉(zhuǎn)90度
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }

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

}

記得需要添加照相機(jī)權(quán)限:

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論