Android怎么实现边采集边上传
1个回答
2015-07-07 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517190
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
请看我下面的代码,我将为大家详细的解释上面的情况出现的理由和解决方案:
package com.camera.CameraPreview;
import java.io.IOException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.Window;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
public class PreviewActivity extends Activity implements Callback, PreviewCallback {
private SurfaceView mSurface ;
private SurfaceHolder mHolder;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//and set the preview to landscape mode.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
mSurface = (SurfaceView)this.findViewById(R.id.SurfacePrewView);
mHolder = mSurface.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//该代码放在addCallback前还是addCallback后对代码Camera有没有影响
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Camera.Parameters parame = mCamera.getParameters();
parame.setPreviewSize(320,480);
mCamera.startPreview();
}
private Camera mCamera ;
@Override
public void surfaceCreated(SurfaceHolder holder) {
if(mCamera!=null)
{
mCamera.release();
mCamera = null ;
}
//<uses-permission android:name="android.permission.CAMERA" />
//必须添加此权限,否则DDMS便会提示你:can not connect to Camera service
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(mHolder);//此代码必须添加视频才会显示出来
mCamera.setPreviewCallback(this);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(mCamera != null)
{
mCamera.release();
mCamera = null ;
}
}
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
// TODO Auto-generated method stub
//get the prew frame here,the data of default is YUV420_SP
//you should change YUV420_SP to YUV420_P
}
}
复制代码
1. 为了确保在所有的平台上你都能够看到画面,SurfaceCreated() SurfaceChanged() SurfaceDestory()请一定要按照上面的写法写:
SurfaceCreated中注意
mCamera.setPreviewDisplay(mHolder);//此代码必须添加视频才会显示出来
mCamera.setPreviewCallback(this);//设置预览帧的接口,就是通过这个接口,我们来获得预览帧的数据的
2.图像是倒的如何解决?
//and set the preview to landscape mode.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
在SetContentView之前调用此代码,理由大家自己看API文档,解释的麻烦.
3.public void onPreviewFrame(byte[] data, Camera camera)
该接口为获取视频预览帧的接口,传递进来的data,默认是YUV420SP的,H264编码的源默认是YUV420的,将他们转换一下再编码图像就正常了;
package com.camera.CameraPreview;
import java.io.IOException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.Window;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
public class PreviewActivity extends Activity implements Callback, PreviewCallback {
private SurfaceView mSurface ;
private SurfaceHolder mHolder;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//and set the preview to landscape mode.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
mSurface = (SurfaceView)this.findViewById(R.id.SurfacePrewView);
mHolder = mSurface.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//该代码放在addCallback前还是addCallback后对代码Camera有没有影响
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Camera.Parameters parame = mCamera.getParameters();
parame.setPreviewSize(320,480);
mCamera.startPreview();
}
private Camera mCamera ;
@Override
public void surfaceCreated(SurfaceHolder holder) {
if(mCamera!=null)
{
mCamera.release();
mCamera = null ;
}
//<uses-permission android:name="android.permission.CAMERA" />
//必须添加此权限,否则DDMS便会提示你:can not connect to Camera service
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(mHolder);//此代码必须添加视频才会显示出来
mCamera.setPreviewCallback(this);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(mCamera != null)
{
mCamera.release();
mCamera = null ;
}
}
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
// TODO Auto-generated method stub
//get the prew frame here,the data of default is YUV420_SP
//you should change YUV420_SP to YUV420_P
}
}
复制代码
1. 为了确保在所有的平台上你都能够看到画面,SurfaceCreated() SurfaceChanged() SurfaceDestory()请一定要按照上面的写法写:
SurfaceCreated中注意
mCamera.setPreviewDisplay(mHolder);//此代码必须添加视频才会显示出来
mCamera.setPreviewCallback(this);//设置预览帧的接口,就是通过这个接口,我们来获得预览帧的数据的
2.图像是倒的如何解决?
//and set the preview to landscape mode.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
在SetContentView之前调用此代码,理由大家自己看API文档,解释的麻烦.
3.public void onPreviewFrame(byte[] data, Camera camera)
该接口为获取视频预览帧的接口,传递进来的data,默认是YUV420SP的,H264编码的源默认是YUV420的,将他们转换一下再编码图像就正常了;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询