Android视频流采集手机录像和PC同步Word文档下载推荐.docx
《Android视频流采集手机录像和PC同步Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《Android视频流采集手机录像和PC同步Word文档下载推荐.docx(17页珍藏版)》请在冰豆网上搜索。
![Android视频流采集手机录像和PC同步Word文档下载推荐.docx](https://file1.bdocx.com/fileroot1/2023-1/2/3d4a0464-c865-4916-aa05-239c129aab3a/3d4a0464-c865-4916-aa05-239c129aab3a1.gif)
importjava.io.DataOutputStream;
import.ServerSocket;
import.Socket;
publicclassMainextendsFrame{
privatestaticfinallongserialVersionUID=1L;
privateBufferedImageim;
//图像信息
//privatefinalintwidth=480;
//privatefinalintheight=320;
privatestaticfinalintwidth=240;
privatestaticfinalintheight=160;
privatestaticfinalintnumBands=3;
privatestaticfinalintdataLen=57600;
//307200OR230400//5760076800
privatestaticfinalinttt=28800;
//14400;
//28800;
//图像数组
privatebyte[]byteArray=newbyte[width*height*numBands];
//图像RGB数组
privatebyte[]yuv420sp=newbyte[dataLen];
//图像YUV数组
privatestaticfinalint[]bandOffsets=newint[]{
0,1,2
};
privatestaticfinalSampleModelsampleModel=newPixelInterleavedSampleModel(
DataBuffer.TYPE_BYTE,width,height,3,width*3,bandOffsets);
//ColorModel
privatestaticfinalColorSpacecs=ColorSpace.getInstance(ColorSpace.CS_sRGB);
privatestaticfinalComponentColorModelcm=newComponentColorModel(cs,false,false,
Transparency.OPAQUE,DataBuffer.TYPE_BYTE);
publicMain(){
super("
Flushing"
);
updateIM();
setSize(480,320);
//窗口关闭方法
this.addWindowListener(newjava.awt.event.WindowAdapter(){
publicvoidwindowClosing(java.awt.event.WindowEvente){
System.exit(0);
}
});
//窗口居中
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
this.getData();
publicvoidupdate(Graphicsg){
paint(g);
publicvoidpaint(Graphicsg){
g.drawImage(im,0,0,480,320,this);
publicvoidgetData(){
try{
System.out.print("
等待连接……\n"
ServerSocketserver=newServerSocket(8899);
Socketsocket=server.accept();
已建立连接!
\n"
DataInputStreamdis=newDataInputStream(socket.getInputStream());
DataOutputStreamdos=newDataOutputStream(socket.getOutputStream());
while(true){
for(inti=0;
i<
dataLen/tt;
i++){
dis.read(yuv420sp,i*tt,tt);
//得到数据之后立即更新显示
im.flush();
repaint();
dos.writeBoolean(true);
}catch(Exceptionex){
ex.printStackTrace();
privatevoidupdateIM(){
//解析YUV成RGB格式
decodeYUV420SP(byteArray,yuv420sp,width,height);
DataBufferdataBuffer=newDataBufferByte(byteArray,numBands);
WritableRasterwr=Raster.createWritableRaster(sampleModel,dataBuffer,
newPoint(0,0));
im=newBufferedImage(cm,wr,false,null);
privatestaticvoiddecodeYUV420SP(byte[]rgbBuf,byte[]yuv420sp,intwidth,intheight){
finalintframeSize=width*height;
if(rgbBuf==null)
thrownewNullPointerException("
buffer'
rgbBuf'
isnull"
if(rgbBuf.length<
frameSize*3)
thrownewIllegalArgumentException("
size"
+rgbBuf.length
+"
<
minimum"
+frameSize*3);
if(yuv420sp==null)
yuv420sp'
if(yuv420sp.length<
frameSize*3/2)
+yuv420sp.length
+frameSize*3/2);
inti=0,y=0;
intuvp=0,u=0,v=0;
inty1192=0,r=0,g=0,b=0;
for(intj=0,yp=0;
j<
height;
j++){
uvp=frameSize+(j>
>
1)*width;
u=0;
v=0;
for(i=0;
width;
i++,yp++){
y=(0xff&
((int)yuv420sp[yp]))-16;
if(y<
0)
y=0;
if((i&
1)==0){
v=(0xff&
yuv420sp[uvp++])-128;
u=(0xff&
y1192=1192*y;
r=(y1192+1634*v);
g=(y1192-833*v-400*u);
b=(y1192+2066*u);
if(r<
r=0;
elseif(r>
262143)
r=262143;
if(g<
g=0;
elseif(g>
g=262143;
if(b<
b=0;
elseif(b>
b=262143;
rgbBuf[yp*3]=(byte)(r>
10);
rgbBuf[yp*3+1]=(byte)(g>
rgbBuf[yp*3+2]=(byte)(b>
publicstaticvoidmain(String[]args){
//Framef=
newMain();
Android端:
Java代码:
importandroid.app.Activity;
importandroid.content.res.Configuration;
importandroid.graphics.PixelFormat;
importandroid.hardware.Camera;
importandroid.os.Bundle;
importandroid.view.SurfaceHolder;
importandroid.view.SurfaceView;
importandroid.view.View;
importandroid.view.Window;
importandroid.view.WindowManager;
importandroid.view.SurfaceHolder.Callback;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.EditText;
publicclassMainActivityextendsActivityimplementsCallback,OnClickListener{
privateSurfaceViewmSurfaceView=null;
privateSurfaceHoldermSurfaceHolder=null;
privateCameramCamera=null;
privatebooleanmPreviewRunning=false;
//连接相关
privateEditTextremoteIP=null;
privateButtonconnect=null;
privateStringremoteIPStr=null;
//视频数据
privateStreamItstreamIt=null;
publicstaticKitkit=null;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
mSurfaceView=(SurfaceView)this.findViewById(R.id.surface_camera);
mSurfaceHolder=mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
remoteIP=(EditText)this.findViewById(R.id.remoteIP);
connect=(Button)this.findViewById(R.id.connect);
connect.setOnClickListener(this);
publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,intheight){
if(mPreviewRunning){
mCamera.stopPreview();
Camera.Parametersp=mCamera.getParameters();
p.setPreviewSize(width,height);
streamIt=newStreamIt();
kit=newKit();
mCamera.setPreviewCallback(streamIt);
mCamera.setParameters(p);
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
mPreviewRunning=true;
publicvoidsurfaceCreated(SurfaceHolderholder){
mCamera=Camera.open();
publicvoidsurfaceDestroyed(SurfaceHolderholder){
mPreviewRunning=false;
mCamera.release();
publicvoidonConfigurationChanged(ConfigurationnewConfig){
super.onConfigurationChanged(newConfig);
if(this.getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE){
}elseif(this.getResources().getConfiguration().orientation==Configuration.ORIENTATION_PORTRAIT){
classKitimplementsRunnable{
privatebooleanrun=true;
//privatefinalintdataLen=57600;
//307200OR23040076800OR57600
privatefinalinttt=28800;
publicvoidrun(){
//TODOAuto-generatedmethodstub
Socketsocket=newSocket("
219.232.191.146"
8899);
//Socketsocket=newSocket("
10.0.2.15"
while(run){
dos.write(streamIt.yuv420