【Android】Android Camera原始帧格式转换 —— 获取Camera图像(一)
package com.dreamy.cameraframedemo; import android.app.Activity;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View; import com.dreamy.cameraframedemo.util.ImageUtils; import java.io.IOException; public class MainActivity extends Activity implements SurfaceHolder.Callback,
Camera.PreviewCallback, View.OnClickListener {
// raw frame resolution: 1280x720, image format is: YV12
// you need get all resolution that supported on your devices;
// my phone is HUAWEI honor 6Plus, most devices can use 1280x720
private static final int SRC_FRAME_WIDTH = 1280;
private static final int SRC_FRAME_HEIGHT = 720;
private static final int IMAGE_FORMAT = ImageFormat.YV12; private Camera mCamera;
private Camera.Parameters mParams;
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setListener();
} private void initView() {
mSurfaceView = (SurfaceView) findViewById(R.id.sv_recording);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.setFixedSize(SRC_FRAME_WIDTH, SRC_FRAME_HEIGHT);
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
} private void setListener() {
// set Listener if you want, eg: onClickListener
} @Override
public void onClick(View v) {
} @Override
public void onPreviewFrame(byte[] data, Camera camera) {
ImageUtils.saveImageData(data);
camera.addCallbackBuffer(data);
} private void openCamera(SurfaceHolder holder) {
releaseCamera(); // release Camera, if not release camera before call camera, it will be locked
mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
mParams = mCamera.getParameters();
setCameraDisplayOrientation(this, Camera.CameraInfo.CAMERA_FACING_BACK, mCamera);
mParams.setPreviewSize(SRC_FRAME_WIDTH, SRC_FRAME_HEIGHT);
mParams.setPreviewFormat(IMAGE_FORMAT); // setting preview format:YV12
mParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
mCamera.setParameters(mParams); // setting camera parameters
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException ioe) {
ioe.printStackTrace();
}
mCamera.setPreviewCallback(this);
mCamera.startPreview();
} private synchronized void releaseCamera() {
if (mCamera != null) {
try {
mCamera.setPreviewCallback(null);
} catch (Exception e) {
e.printStackTrace();
}
try {
mCamera.stopPreview();
} catch (Exception e) {
e.printStackTrace();
}
try {
mCamera.release();
} catch (Exception e) {
e.printStackTrace();
}
mCamera = null;
}
} /**
* Android API: Display Orientation Setting
* Just change screen display orientation,
* the rawFrame data never be changed.
*/
private void setCameraDisplayOrientation(Activity activity, int cameraId, Camera camera) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int displayDegree;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
displayDegree = (info.orientation + degrees) % 360;
displayDegree = (360 - displayDegree) % 360; // compensate the mirror
} else {
displayDegree = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(displayDegree);
} @Override
public void surfaceCreated(SurfaceHolder holder) {
openCamera(holder); // open camera
} @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <SurfaceView
android:id="@+id/sv_recording"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
别忘了在AndroidManifest中打开Camera的权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera" />
package com.dreamy.cameraframedemo.util; import android.os.Environment; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date; /**
* Created by raomengyang on 4/25/16.
*/
public class ImageUtils {
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2; // save image to sdcard path: Pictures/MyTestImage/
public static void saveImageData(byte[] imageData) {
File imageFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (imageFile == null) return;
try {
FileOutputStream fos = new FileOutputStream(imageFile);
fos.write(imageData);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} public static File getOutputMediaFile(int type) {
File imageFileDir =
new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyTestImage");
if (!imageFileDir.exists()) {
if (!imageFileDir.mkdirs()) {
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File imageFile;
if (type == MEDIA_TYPE_IMAGE) {
imageFile = new File(imageFileDir.getPath() + File.separator +
"IMG_" + timeStamp + ".jpg");
} else if (type == MEDIA_TYPE_VIDEO) {
imageFile = new File(imageFileDir.getPath() + File.separator +
"VID_" + timeStamp + ".mp4");
} else return null;
return imageFile;
}
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_dreamy_jni_LibyuvUtils */ #ifndef _Included_com_dreamy_jni_LibyuvUtils
#define _Included_com_dreamy_jni_LibyuvUtils
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_dreamy_jni_LibyuvUtils
* Method: initRotateNV21
* Signature: (II)V
*/
JNIEXPORT void JNICALL Java_com_dreamy_jni_LibyuvUtils_initRotateNV21
(JNIEnv *, jclass, jint, jint); /*
* Class: com_dreamy_jni_LibyuvUtils
* Method: ScaleYV12ToI420
* Signature: ([B[BIIIII)V
*/
JNIEXPORT void JNICALL Java_com_dreamy_jni_LibyuvUtils_ScaleYV12ToI420
(JNIEnv *, jclass, jbyteArray, jbyteArray, jint, jint, jint, jint, jint); /*
* Class: com_dreamy_jni_LibyuvUtils
* Method: ReleaseRotateNV21
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_dreamy_jni_LibyuvUtils_ReleaseRotateNV21
(JNIEnv *, jclass); #ifdef __cplusplus
}
#endif
#endif
资源下载:
YUV格式查看工具:RawViewer:http://download.csdn.net/detail/zxccxzzxz/9508288
项目地址:https://github.com/eterrao/BlogExamples.git
【Android】Android Camera原始帧格式转换 —— 获取Camera图像(一)的更多相关文章
- android 中如何获取camera当前状态
/** * 测试当前摄像头能否被使用 * * @return */ public static boolean isCameraCanUse() { boolean canUse = true; Ca ...
- [Android]android.graphics.Camera实现图像的旋转、缩放,配合Matrix实现图像的倾斜
android.graphics.Camera可以对图像执行一些比较复杂的操作,诸如旋转与绽放,与Matrix可实现图像的倾斜. 个人总结Camera与Matrix的一些区别如下: Camera的ro ...
- Android 音视频开发(四):使用 Camera API 采集视频数据
本文主要将的是:使用 Camera API 采集视频数据并保存到文件,分别使用 SurfaceView.TextureView 来预览 Camera 数据,取到 NV21 的数据回调. 注: 需要权限 ...
- 【转】Android Camera(五)使用Camera功能 AREA的理解
http://blog.csdn.net/think_soft/article/details/7998478 使用Camera功能 大多数的Camera功能都是使用Camera.Parameters ...
- Android tabhost下的activity怎样获取传来的值
android tabhost下的activity怎样获取传来的值,具体解决方案如下: 解决方案: 其他activity设置intent:Intent intent=new Intent(); int ...
- Android 4.0以后正确的获取外部sd卡存储目录
刚解决这个棘手的问题 找了很久,随笔记下. 网上搜索 android 获取外部sd卡存储目录 普遍都是: 1) Environment.getExternalStorageDirectory() 这个 ...
- Android如何在初始化的时候获取加载的布局的宽高
在自定义ListView中,需要将下拉刷新的View在初始化的时候设置padding隐藏起来,这时就要在初始化的时候获得要加载的布局View的高度. private View headView; he ...
- Android中从SD卡中获取歌词并与歌曲同步
先看看效果图吧,再看代码 转换文件的编码格式 package com.xm; import java.io.BufferedInputStream; import java.io.BufferedRe ...
- Android ADB工具-操作手机和获取手设备信息(四)
Android ADB工具-操作手机和获取手设备信息(四) 标签(空格分隔): Android ADB 6. 其它命令 命令 功能 adb shell input text <content&g ...
随机推荐
- springMVC静态文件访问
web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xs ...
- Eclipse无法启动错误之Ensure that the org.eclipse.core.runtime bundle is resolved and started (see config.ini)
悲剧,在安装Android Build Tools时,提醒需要关闭Eclipse进行安装,于是我在Tools安装完成后重启了Eclipse.但是Eclipse却无法启动,在log中有如下提示: Una ...
- python 多线程和多进程基本写法
#coding=utf-8 def aJob(arg): """ 提供给多线程调用 """ import threading t = thr ...
- [ASE][Daily Scrum]11.28
昨天基本上已经完成了sprint2的task, 现在剩下一些bug还需要来修正, 然后我正式加入码代码的大军啦~ Shilin Liu 显示聊天框 Zhao Li 搭建聊天服务器 Yimin ...
- diff详解,读懂diff结果
1.概述 本文将要讨论的是diff命令,diff用来比较两个文件.当然文件比较的工具很多,windows系统下面就有不错的工具可以使用,例如常用的Beyond Compare,WinMerge都是图形 ...
- 循序渐进做项目系列(2):最简单的C/S程序——消息异步调用与消息同步调用
上篇博客 循序渐进做项目系列(1):最简单的C/S程序——让服务器来做加法 实现了一个最简单的C/S程序,即让服务器来做加法.当时为了通俗易懂采用了消息异步调用的方式.今天我们要采用消息同步调用的方式 ...
- note of introduction of Algorithms(Lecture 3 - Part1)
Lecture 3(part 1) Divide and conquer 1. the general paradim of algrithm as bellow: 1. divide the pro ...
- Windows Phone 8.1上的开发人员请看
1)SDK选择:如果你是在Windows Phone 8.1上做一个新App, 或者想把7.x/8.0的App移植到8.1上,请使用WinRT SDK,而不是Silverlight.当然Silverl ...
- Linux1:Linux概述
为什么服务器尤其大型服务器都使用Linux系统 服务器尤其是大型服务器一般都使用Linux系统,有以下几点原因: 1.成本低,Linux操作系统是免费的 2.安全性好,Linux采取了许多的安全措施, ...
- Aoite 系列(03) - 一起来 Redis 吧!
Aoite 是一个适于任何 .Net Framework 4.0+ 项目的快速开发整体解决方案.Aoite.Data 适用于市面上大多数的数据库提供程序,通过统一封装,可以在日常开发中简单便捷的操作数 ...