`

照相机2-----调用函数

 
阅读更多

 

第二种方法,利用Camera API进行调用照相机

1.首先需要添加权限:

<uses-permission android:name=”android.permission.CAMERA”>

2.运行结果,图片旋转了90度,需要在ACTIVITYonCreate的当下旋转Activity的方向:

This.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

 

package com.cameraapi;

 

 

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.List;

 

 

import android.os.Bundle;

import android.os.Environment;

import android.app.Activity;

import android.content.pm.ActivityInfo;

 

import android.graphics.PixelFormat;

 

 

import android.hardware.Camera;

import android.hardware.Camera.CameraInfo;

import android.hardware.Camera.PictureCallback;

import android.hardware.Camera.ShutterCallback;

import android.hardware.Camera.Size;

 

import android.media.CamcorderProfile;

import android.media.MediaRecorder;

import android.media.CameraProfile;

 

import android.util.DisplayMetrics;

import android.util.Log;

 

 

import android.view.View;

import android.view.Window;

import android.view.Menu;

import android.view.Surface;

import android.view.SurfaceHolder;

import android.view.WindowManager;

import android.view.SurfaceHolder.Callback;

import android.view.SurfaceView;

import android.view.View.OnClickListener;

 

import android.widget.Button;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.Toast;

 

publicclass MainActivity extends Activity implements SurfaceHolder.Callback{//

 

 

publicstaticfinalintMEDIA_TYPE_IMAGE = 1;

publicstaticfinalintMEDIA_TYPE_VIDEO = 2;

 

private Camera mCamera01;

private Button mButton01, mButton02, mButton03;//, mButton04

private SurfaceView mSurfaceView01;

private SurfaceHolder mSurfaceHolder01;

 

privatebooleanbIfPreview = false;

privatebooleanrecord_stop = true;

 

private MediaRecorder mRecorder01;

 

@Override

protectedvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

 

//设置窗口模式标题、全屏等性质

requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);// 设置全屏.FLAG_FULLSCREEN

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

getWindow().setFormat(PixelFormat.JPEG);

setContentView(R.layout.activity_main);

init_msys();// 初始化各种控件、参数

}

 

privatevoid init_msys() {

 

mButton01 = (Button)findViewById(R.id.button1);// START/STOP

mButton02 = (Button)findViewById(R.id.button2);//FINISH

mButton03 = (Button)findViewById(R.id.button3);//transmit

//设定按钮监听函数

mButton01.setOnClickListener(new myButtonClickListener());

mButton02.setOnClickListener(new myButtonClickListener());

mButton03.setEnabled(false);

 

mSurfaceView01 = (SurfaceView) findViewById(R.id.surfaceView1);

mSurfaceHolder01 = mSurfaceView01.getHolder();// 获取 holder

mSurfaceHolder01.addCallback(this);//加入回调接口

mSurfaceHolder01.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

 

}

 

class myButtonClickListener implements OnClickListener{

 

@Override

publicvoid onClick(View v) {

// TODO Auto-generated method stub

//开始停止录像按钮

if(v == mButton01)

{

if(record_stop){

init_camera();

mButton01.setText("停止");

record_stop=false;

}else

{

stop_camera();

mButton01.setText("开始");

record_stop=true;

}

}

//按结束按钮,退出摄像机

if(v== mButton02){

finish();

}

}

}

 

privatevoid init_camera()

{if(!bIfPreview)

{

//mCamera01 = Camera.open();

if(mCamera01 == null)

Toast.makeText(MainActivity.this, "没有打开摄像头", Toast.LENGTH_SHORT).show();

else

Toast.makeText(MainActivity.this, "打开了摄像头", Toast.LENGTH_SHORT).show();

Camera.Parameters params = mCamera01.getParameters();

List<Size> sizes = params.getSupportedPictureSizes();

 

for (Size size : sizes) {

Toast.makeText(MainActivity.this, "\r\n w:"+size.width+";h:"+size.height, Toast.LENGTH_SHORT).show();

//et.append("\r\n w:"+size.width+";h:"+size.height);

}

mCamera01.stopPreview();

mCamera01.unlock();

mRecorder01 = new MediaRecorder();// 创建mRecorder对象

mRecorder01.setCamera(mCamera01);// 设置录制视频源为Camera(相机)

mRecorder01.setVideoSource(MediaRecorder.VideoSource.CAMERA); //.DEFAULT

mRecorder01.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);//MediaRecorder.AudioSource.MIC

 

mRecorder01.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

// 设置录制完成后视频的封装格式THREE_GPP3gp.MPEG_4mp4

// mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

// 设置录制的视频编码h263 h264

//mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

//mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

// 设置视频录制的分辨率。必须放在设置编码和格式的后面,否则报错

//mediarecorder.setVideoSize(352, 288);

// 设置录制的视频帧率。必须放在设置编码和格式的后面,否则报错

//mediarecorder.setVideoFrameRate(15);

 

mRecorder01.setPreviewDisplay(mSurfaceHolder01.getSurface());

mRecorder01.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());

 

try {

// 准备录制

mRecorder01.prepare();

// 开始录制

mRecorder01.start();

} catch (IllegalStateException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

bIfPreview = true;

}

}

 

privatestatic File getOutputMediaFile(int type){

File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(

Environment.DIRECTORY_DCIM), "mypictures");

if (! mediaStorageDir.exists()){

if (! mediaStorageDir.mkdirs()){

Log.d("mypictures", "failed to create directory");

returnnull;

}

}

// Create a media file name

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

File mediaFile;

if (type == MEDIA_TYPE_IMAGE){

mediaFile = new File(mediaStorageDir.getPath() + File.separator +

"IMG_"+ timeStamp + ".jpg");

} elseif(type == MEDIA_TYPE_VIDEO) {

mediaFile = new File(mediaStorageDir.getPath() + File.separator +

"VID_"+ timeStamp + ".mp4");

} else {

returnnull;

}

 

return mediaFile;

}

 

privatevoid stop_camera(){

bIfPreview = false;

if (mRecorder01 != null){

mRecorder01.stop();

mRecorder01.reset();

mRecorder01.release();

mRecorder01 = null;

mCamera01.lock();

}

}

 

 

@Override

publicboolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

returntrue;

}

 

@Override

publicvoid surfaceChanged(SurfaceHolder holder, int format, int width,

int height) {

// TODO Auto-generated method stub

 

}

 

@Override

publicvoid surfaceCreated(SurfaceHolder holder) {

// TODO Auto-generated method stub

mSurfaceHolder01 = holder;

if(null == mCamera01)

mCamera01 = Camera.open();

}

 

@Override

publicvoid surfaceDestroyed(SurfaceHolder holder) {

// TODO Auto-generated method stub

stop_camera();

mCamera01.stopPreview();

mCamera01.release();

mCamera01 = null;

 

// mCamera01.release();

}

}

 

 

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

 

<SurfaceView

android:id="@+id/surfaceView1"

android:layout_width="301dp"

android:layout_height="250dp"

android:layout_gravity="center_horizontal" />

 

<RelativeLayout

android:layout_width="300dp"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:gravity="center_horizontal" >

 

<Button

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:text="录像" />

 

<Button

android:id="@+id/button3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_toRightOf="@+id/button1"

android:text="连接" />

 

<Button

android:id="@+id/button4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_toRightOf="@+id/button3"

android:text="远程" />

 

<Button

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_toRightOf="@+id/button4"

android:text="退出" />

 

</RelativeLayout>

 

</LinearLayout>

 

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.cameraapi"

android:versionCode="1"

android:versionName="1.0" >

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name="com.cameraapi.MainActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

 

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

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

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

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

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

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

<uses-feature android:name="android.hardware.camera" />

<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />

</manifest>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics