压缩方法采用的是Bitmap Compress方法
package com.example.administrator.compress; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.PixelFormat; import android.hardware.Camera; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.nio.ByteBuffer; public class MainActivity extends Activity { private Camera mCamera = null; private SurfaceHolder holder = null; private Button button1,button2; private int width = 640; private int height = 480; ServerSocket server = null; Socket socket = null; private static final int PORT = 5555; DataOutputStream out = null; private byte []rgb_data = new byte[width*height*4]; ByteArrayOutputStream baos = null; Bitmap VideoBit = null; private ByteBuffer byteBuffer = null; ImageView imageView = null; class Callback implements Camera.PreviewCallback { @Override public void onPreviewFrame(byte[] frame, Camera camera) { decodeYUV420SP(rgb_data,frame,width,height); VideoBit = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888); byteBuffer = ByteBuffer.wrap(rgb_data); byteBuffer.position(0); VideoBit.copyPixelsFromBuffer(byteBuffer); Matrix matrix = new Matrix(); matrix.postRotate(90); baos = new ByteArrayOutputStream(); VideoBit.compress(Bitmap.CompressFormat.JPEG,1,baos); byte []bytes = baos.toByteArray(); imageView.setImageBitmap(Bitmap.createBitmap(BitmapFactory.decodeByteArray(bytes,0,bytes.length),0,0,width,height,matrix,true)); try{ out.write(frame); out.flush(); }catch (IOException e){ e.printStackTrace(); } } } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SurfaceView mSurfaceView = (SurfaceView) this.findViewById(R.id.camera_preview); button1 = (Button) findViewById(R.id.b1); button2 = (Button) findViewById(R.id.b2); holder = mSurfaceView.getHolder(); imageView = (ImageView)findViewById(R.id.imageView); new MyThread().start(); button1.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { if(mCamera == null) { mCamera = Camera.open(); Camera.Parameters p = mCamera.getParameters(); p.setPreviewFormat(PixelFormat.YCbCr_420_SP); p.setPreviewSize(width,height); p.setPreviewFrameRate(15); //设置帧率 mCamera.setParameters(p); mCamera.setDisplayOrientation(90); try { mCamera.setPreviewDisplay(holder); } catch (IOException e) { e.printStackTrace(); } mCamera.startPreview(); Callback a = new Callback(); mCamera.setPreviewCallback(a); } } }); button2.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { finish(); } }); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } @Override public void finalize() { try { super.finalize(); } catch (Throwable e) { e.printStackTrace(); } } public class MyThread extends Thread{ public void run(){ try{ server = new ServerSocket(PORT); socket = server.accept(); out = new DataOutputStream(socket.getOutputStream()); }catch (IOException e){ e.printStackTrace(); } } } static public void decodeYUV420SP(byte[] rgb, byte[] yuv420sp, int width, int height) { final int frameSize = width * height; for (int j = 0, yp = 0; j < height; j++) { int uvp = frameSize + (j >> 1) * width, u = 0, v = 0; for (int i = 0; i < width; i++, yp++) { int y = (0xff & ((int) yuv420sp[yp])) - 16; if (y < 0) y = 0; if ((i & 1) == 0) { v = (0xff & yuv420sp[uvp++]) - 128; u = (0xff & yuv420sp[uvp++]) - 128; } int y1192 = 1192 * y; int r = (y1192 + 1634 * v); int g = (y1192 - 833 * v - 400 * u); int b = (y1192 + 2066 * u); if (r < 0) r = 0; else if (r > 262143) r = 262143; if (g < 0) g = 0; else if (g > 262143) g = 262143; if (b < 0) b = 0; else if (b > 262143) b = 262143; rgb[yp*4] = (byte)(r >>10); rgb[yp*4+1] = (byte)(g >>10); rgb[yp*4+2] = (byte)(b >> 10); rgb[yp*4+3] = (byte)255; } } } } <?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" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="50px" android:text="图像采集测试" android:gravity="center" /> <SurfaceView android:id="@+id/camera_preview" android:layout_width="480px" android:layout_height="640px" > </SurfaceView> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/b1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30px" android:text="采集" android:gravity="center" /> <Button android:id="@+id/b2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30px" android:text="退出" android:gravity="center" /> </LinearLayout> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher" android:id="@+id/imageView" /> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.administrator.compress"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <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.INTERNET"/> <uses-permission android:name="android.permission.CAMERA"/> </manifest>