安卓干货——PC 连接手机实现摄像头预览

本文是一篇干货,我连配图都没放!

1 功用需求

(1)手机端翻开摄像头实时预览;

(2)手机端作为服务端,PC端作为客户端联接;

(3)联接成功后PC端能够一起预览手机端的摄像头收集的图画;

(4)PC端点击摄影能够操控手机端摄影一张相片,并将相片传给PC端。

2 功用模块

(1)安卓手机java面试题翻开摄像头并结束预览和摄影功用;

(2)手机端翻开监听,并在联接成功后将摄像头收集的数据传给PC;

(3)手机端读取PC发送的指令指令,履行相应的操作。

3 开发流程

3.1 翻开摄像头结束预览

(1) 获取摄像头权限,并增加主动对焦特色。
  在应用程序的majava面试题nifest.xml文件中添面试问题大全及答案大全加运用手机摄像头权限。因为程序需求运用socket的通讯进程,将手机作为面试问题大全及答案大全服务端,因而需求增加网络权限。

 <uses-permission android:name="android.permission.CAMERA"java面试题 />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.ca面试毛遂自荐mera.autofocjavaeeus" />
<uses-permission Android:name="androjava面试题i面试毛遂自荐d.permission.INTERShellNET"/>

2)结束相机的实时预览。
   安卓体系运用SurfaceView即可结束预览功用。在布局面试技巧和注意事项文件中增加SurfaceView。aandroid竞赛专用包ctivity_main.xml布局文件如下:

<LinearLayout xmlns:android="http://schemas.andro笔记id.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layou面试毛遂自荐一分钟t_width="match_parent"
android:layout_height="match_pashellfishrent"
android:background="@color/gray_light"
android:orientation="vertical" >
<SurfaceView面试
android:id="@shell脚本入门+id/surview"
android:layout_width="f面试问题大全及答案大全ill_parent"
android:layout_height="fill_parent"
android:sc笔记aleTjava面试题ype="fitCenter" />
</LinearLjava难学吗ayout>

(3)在MainActivity 的onCreate中初始化SurfceView。初始化办法如下:

private void initSurfaceView() {
surfaceView = (SurfaceView) findViewById(R.id.surview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.setType(SurfaceHolandroid是什么手机牌子der.SURFACE_TYPE_PUSH_BUFFERS);面试必问10大问题答复
surfaceHolder.setKeepScreenOn(true);
surfaceHoldeshellyr.addCallback(new Callback() {
@Override
public void sur面试常见问题及答复技巧faceDestroyed(SurfaceHojavascriptlder arg0) {
}
@O面试毛遂自荐verrid面试e
public void surfaceC笔记reated(SurfaceHolder arg0) {
// 翻开摄像头
startCamera(curCameraIndex);
}
@Override
public void surfacejava面试题Changed(Sushell脚本编程100例rfaceHolder arg0, int arg1, int arg2,
int arg3) {
}
});
surfaceVi笔记本电脑排名前十ew.setFocusable(true);
surfaceVshell脚本根本指令iew.setBandroid手机ackshell脚本编程100例groundColor(TRIM_MEMORY_BACKGROUND);
}

4)SurfceView创立结束时调用翻开摄像头的办法startCamera()。startCamera办法如下,其间代码意义已在注释程序中注明。

// 依据索引初始化摄像头
@SuppressLint("NewApi")
public void startCamera(int cameraIndex) {
// 先连续摄像头
stopCamera(java初学);
// 再初始化并翻开摄像头
if (camera == null) {
//翻开手机摄像头,cameraIndex为摄像头索引,0代面试必问10大问题答复表后置摄像头,1代表前置摄像头。
camera = Camera.open(cameraIndex);
//创立摄像头操作东西类
cameraUtil = neshell脚本w CameraUtil(camera, callshell脚本根本指令back);
//翻开预览
cameraUtil.initCamera(srcFrjavaeeameHeight, srcFrameWidth, surandroid平板电脑价格fa笔记本电脑开机黑屏没反应怎样办ceHolder);
Log.e(TAG, "翻开相机");
}
}
// 连续并开释摄像头
public void stopCamera() {
if (camera != nulshell编程l) {
camera.setPreviewCallback(null);
camera.stopPreview();
camera.release();
camera = null;
}
}
//摄像头翻开预览后收集到的数据回调接口
Previe面试技巧wCajava初学llback caAndroidllback = new PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
Size size = camera.getParajava难学吗meters().getPreviewSize();
try {
if (times == 0) {java难学吗
YuvImage image = new YuvImage(data, ImageFormat.NV21,
size.width, size.height, null);
if (image != null) {
// 将YuvImage方针转为字节数组
ByteAjava工作培训班rrayOutputStreandroid是什么手机牌子am outputStream = new ByteArrayOutputStream();
image.com面试毛遂自荐简略大方pressToJpeg(new Rect(shell编程0, 0, size.width,
size.height), 100, outputStream);
byte[] srcData = outshell的意思putStream.toByteArray();
int len = srcData.length;
// 字节数组转为Bitmap
Bitmap src = BijavaeetmapF笔记本电脑怎样连wifiactory.decodeByteArray(srcData, 0,
len);
src = BitmapUtil.rotate(src, 90);
// 紧缩Bitmap,并获取紧缩后的字节数组,即可获取预览数据文件
// outdata数据就是待发送给PC端的数据
byte[] outdata = BitmapUtil.transImage(src,
srcFrameWjava模拟器idth / 4, srcFrameHeight / 4);
int dataljava难学吗en = outdata.length;
if (isOpen) {
// 写入头
sendData(new byte[] { (byte) 0xA0 });
// 写入数组长度
sendData(intToByteArray(dashell脚本编程100例talen));
// 写入数据值
sendData(outdata);
}
// 回收Bitmap
if (!src.isRecycled()) {
src.recycle();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
};

(5)CameraUtil为设置摄像android手机头的辅佐类,代码如下:

public class CameraUtil {
//摄像头类
Camera camera;
int ca笔记本cpu天梯图meraIndex;
//预览视图宽和高
int s面试毛遂自荐rcFrameWidth;
int srcFramjavaapi中文在线看eHeight;
SurfaceHolder surfaceHolder;
PreviewCallback callback;
public CameraUtil(Camera camera, PreviewCallback callback) {
this.camera = camera;
tjavaeehis.callback = callback;
}
//摄像头预览初始化
public vo笔记id initCamera(final int srcFrameWidth, final int srcFrameHeight, final SurfaceHolder surfaceHolder) {
this.srcFrameHeight = srcFrameHeight;
this.srcFrameWidth = srcFrameWidth;
this.surfaceHolder = surfaceshellyHolder;
Camera.Parameters params = camera.getParameters();
//params.setPreviewSize(src笔记FrameWidth / 4, srcFrameHeight / 4Android);
//设置预览格局
params.setPreviewFormat(ImageFormat.NV21);
params.setPreviewFrameRate(30);
//设置图画质量
params.setJpegQuality(100);
params.setPictureFor面试毛遂自荐一分钟mat(ImageForAndroidmat.JPEG);
//设置预览方向
params笔记本.set("orientation", "portrait");
params.set("rotatio笔记n", 90);
//设置对焦形式为主动接连对焦
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONT面试INUOUS_PICTURE);// 1接连对焦
camera.setParam面试必问10大问题答复eters(pajava难学吗rams);
camera.se面试毛遂自荐简略大方tDisplayOrientation(90);
// 设置闪现图画的SurfaceView
try {
camera.setPreviewDisp笔记本电脑开机黑屏没反应怎样办lay(surfaceHolder);
} catch (IOException e) {
e.printStackTrace();
}
candroid什么意思amera.setPreviewCallShellback(callback);
//建议预览
camera.startPreview();
camera.autoFocus(new AutoFocusCallback() {
@Override
public void onAutoFocus(boolean result, Camera camera) {
// 主动对焦结束时回调
if (result) {
initCamera(srcFrameWidth, srcFrameHeight, surfaceHolder);
camera.cancelAutoFocus();
}
}
});
}
@SuppressLint("NewApi")
public void startCamera(int ca笔记本电脑meraIndex) {
this.cameraIndex = cameraIndex;
// 先连续摄像头
stopCamera();
// 再初始化并翻开摄像头
if (camera == null) {
camera = Camera.open(cameraIndex);
initCamera(srcFrameWidth, srcFramjava面试题eHeight, surfaandroid的drawable类ce面试毛遂自荐Holder);
}
}
//连续预览
public void stopCamera() {
if (cameraandroid手机 != null) {
camera.setPreviewCalandroid手机lback(null);
camera.stopPreview();
camera.release();
camera = null;
}
}
}

6)笔记本电脑怎样连wifiBitmapUtil为图片操作辅佐类

publ面试毛遂自荐3分钟通用ic clasjava工作培训班s BitmapUtil {
// Bitmap按照必定巨细转为字节数组Java,以便写入sockejava难学吗t进行发送
public static byte[android的drawable类] transImage(Bitmap bjava模拟器itmap, int width, int height) {
// bitmap = adjustPhotoRotation(bitmshell脚本入门ap, 90);
try {
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bjavaeeitmap.getHeight();
float scaleWidth = (float) wijava怎样读dth / bitmapWidth;
float scaleHeight = (float) height / bitma笔记本电脑开不了机pHeight;
Ma面试技巧trix matrshell脚本根本指令ix = new Matrixshellfish();
matrix.postScale(sca面试技巧leWidth, scaleHeight);
// 创立紧缩后的Bitmap
Bitmap resizeBitemp = Bitmap.createBitmap(bitmap, 0,javaee 0,
bitmapWidth, bitmandroid的drawable类apHeight, matrix, false);
// 紧缩图片质量
ByteArrayOutputStreaShellm output面试技巧Strea笔记本电脑怎样连wifim = newandroid/yunos ByteArrayOutputjava言语Strandroid的drawable类eam();
resizshell怎样读eBitemp.compress(Compresjava初学sFormat.JPEG, 80, outputStream);
// 转为字节数组
byte[] byteArray = outputStream.toByteArray();
out笔记putStream.close();
// 回收资源
if (!bitmap.isRecycled()) {
bitmap.recycle();
}
if (!resizeBitempjavaapi中文在线看.isRecycled()) {
rejavaapi中文在线看sizeBitem面试问题大全及答案大全p.re笔记本cycl面试毛遂自荐e();
}
return byteArshellyray;
} catch (Excepandroid竞赛专用包tandroid是什么手机牌子ion ex) {
ex.p笔记本cpu天梯图rintStackTrace();
}
return n笔记本电脑开不了机ull;
}
//图片旋转操作
public static Bitmap rotate(Bitmap bitmap, float degree) {
M面试技巧和注意事项atrix笔记本电脑开机黑屏没反应怎样办 matrix = new Matrix();
// maandroid的drawable类trix.setScale(0.面试问题大全及答案大全5f, 0.5f);// 缩小为本来的一半
matrix.postRotajava工作培训班te(degree);// 旋转45度 == matrix.setSinCos(0.5f, 0.5f);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.ge笔记tW面试毛遂自荐简略大方idth(),
bitmap.getHe面试技巧ight(), matrix, true);
rejava难学吗turn bijava工作培训班tmap;
}
}

3.2 联接手机与PC

手机与PC的联接实际上是运用socket通讯结束shell是什么意思中文的。将手机端作为服务器翻开监听。PC端建议联接,经过adb指令的端口转化将PC的联接请求发送至手机,树立手机与PC的联接。首要操作流程如下:

(1) onCreate中注册播送的监听
注册播送监听首要是接纳PC端调用adb发送的播送信息,用于告知手机端翻开监听和连续监听。

IntentFilter filter = new IntentFilter();
filter.addAction("NotifyServ面试毛遂自荐iceStart");
filter.addAction("面试常见问题及答复技巧NotifyServiceStop");
registerReceiver(receiver, filter);

(2) 接纳体系播送

pub面试问题lic class ServiceBroadcastRece笔记本iver extends Broadjava怎样读castReceiver {
privateshell脚本根本指令 s面试必问10大问题答复tatic final String START_ACTION = "NotifyServiceStart";
private static final String STOP_ACTION = "NotifySer面试技巧viceStop";
@Override
public voandroid下载idandroid下载 onRshellfisheceive(Context context, Intent intent) {
String action = intent.getAJavaction();
if (START_ACTION.android下载equalsIgnoreCa笔记本cpu天梯图se(action)) {
// 建议服务
Log.e(javaapi中文在线看TAG, "收到播送信息建议监听");
new Thread() {
public void run() {
if (serverSocshell的意思ket != null) {
try {
serverSocandroid下载ket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//服务端建议监听
doListen();
};
}.start();
} else if (STOP_ACTION.equalsIgnoreCase(action)) {
}
}
}

(3)建议监听
手机android手机端作为服务端,建议监听。留意关于网络的操作不能在主线程中。建议监听的办法doLisandroid下载安装ten笔记本电脑开机黑屏没反应怎样办()如下:

// 建议服务器端监听
private void doListen() {
serverSocket = null;
try {
serverSocket = new ServerSocket(SERVER_PORT);
while (true) {
Socket socket = serverSockshell的意思et.accept();
Log.e(TAG, "监听到设备联接,建议通讯线程");
//PC端联接成功,建议通讯线程ThreadReadWriterIOSocket
thandroid什么意思readSocket = new ThreadReadWriterIOSocjava模拟器ket(socket笔记本电脑性价比排行);
new Thread(threadSocket).start();
}
} catch (IOException e) {
Log.e(TAG, "服务端监听失利");
e.printStackTrace();
}
}

4)Threadandroid体系ReadWriterIOSocket为担任通讯的线程。
ThreadReadWriterIOSocket是结束了Runnable接口的一个线程,其首要任务就是接纳PC端发送的指令信shell脚本编程100例息,并将指令信息经过EventBus结构发送至主界面。

public class ThreadReadWriterIOSocket implements Runnable {
private stat面试问题大全及答案大全ic Strandroid是什么手机牌子ing TAG = "ThreadReadWriterIOSocket";
private Socket client;
private BufferedOutputStream out;
private BufferedInputSjava工作培训班tream in;
boolean isCandroid什么意思onnecting = false;Shell
private String cmd = "android体系";
public Threjava言语adReadWriteshell怎样读rIOSocket(Socket client) {
this.client = client;
}
@Override
public void run() {
Logandroid竞赛专用包.e(java言语TAG, "有客户端联接上");面试常见问题及答复技巧
isConnecting = true;
try {
// 获取输入输出流
out = new BufferedOutputStream(client.getOutputStream());
in  = new BufferedInputStream(clie笔记本电脑开机黑屏没反应怎样办nt.getInputStream());
//笔记 循环等候,接受PC端的指令
whi笔记本cpu天梯图le (isConnecandroid是什么手机牌子ting) {
try {
if (!client.isConn笔记本电脑开不了机ected()) {
breakjavaee;
}
// 读取指令
cmd = readCMDFromSocket(in);
Log.e(TAG, "读取到PC发送的指令" + cmjavaapi中文在线看d);
/* 依据指令分别处理数据 */
if (cmd.equals(Constant.CONNECT)) {// 收到联接指令
EventBus.getDefault().post(new MessageEvent笔记本电脑(Constant.START))java初学;
out.flush();
} else if (cmd.equalsIgnoreCase(Constant.DISCONNECT)) {// 断开指令
EventBus.getDefault().post(new MessageEvent(Constant.STOP));
out.flush();
}else if (cmd.equals(Consshell编程t    ant.TAKEP面试毛遂自荐HOTO)) {//android/yunos摄影指令
EventBus.getDefault().post(面试问题大全及答案大全new MessageEvent(Constant.TAKEPHOTO));
out.flush(android是什么手机牌子);
}
in.reset();
} catch (Exception e) {
e.prshell脚本入门intStaandroid/yunosckTrace();
}
}
out.close();
in.close();
} catch (Exceshell的意思ption e) {
e.printStackTrace();android/yunos
}
}
public void cancel() {
isConnec笔记本电脑开机黑屏没反应怎样办ting = fandroid/yunosalse;
}
//发送数据
public void writeData(byte[] data) {
if (out != null) {笔记本电脑开不了机
try {
out.write((data));
} catch (IOException e) {
Log.e(TAG, "android手机输入输出失常");
e.printStackT面试问题大全及答案大全race();
}
}
}
/* 读取指令 */
public String readCMDFromSocket(InputStream in) {
int MAX_BUFFER_BYTES = 2048;
String msg = "";
byte[] tempbuffer = new笔记本 byte[MAXjavaapi中文在线看_BUFFAndroidER_BYTES];
try {
int numReadedBytes = in.read(tempbuffer, 0, tempbuffer.length);
msg = new String(tempbuffer,shell编程 0, numReadedBytes, "utfshell脚本入门-8");java怎样读
tempbuffer = null;
} catch (Exception e) {
Log.e(TAG, "reashellfishdCMDFromSocket读数失常" + e.toString());
EventBus.getDefault().post(new MessageEventjava工作培训班(Constant.DISCONNECT));
e.printStackTrace();
}
returshell脚本编程100例n msg;
}
}

(5)主界面接纳EventBus传递的指令信息
通讯的告知进程选用EventBus结束,android手机在主界面面试必问10大问题答复中注册EventBus。
onCreate中注册EventBus

EventBus.getDefault().register(this);

创立onMessageEvent办法结束工作处理函数,并将工作处理交给主线程。

@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(Meandroid什么意思ssageEvent event) {
switch (event.message) {
case Constant.DISCONNECT://断开指令
Toast面试问题大全及答案大全.makeText(Main笔记本电脑什么牌子好Activity.this, "客户java工作培训班端断开", Toast.LENGTH_LONG)
.show(面试必问10大问题答复);
threadSocket.canc笔记el();
case Constant.START://收到联接指令
isOpen = true;
Toast.makeText(MainActivity.this, "客户端联接上", Toast笔记本电脑性价比排行.LENGTH_LONG面试)
.show();
startCamerajava工作培训班(curCameraIndex);
break;
case Constantshellfish.STOP://收到断开指令
stopCamera();
isOpen = false;
break;
case Con面试毛遂自荐stant.TAK面试问题EPHOTO://收到摄影指令
if (isOpen) {
camera.takePicture(new ShutterCallback() {
@Override
pu面试常见问题及答复技巧blic void onShutter() {
}
}, newandroid/yunos Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
}
}, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// 向电脑端发送数据
int datalen = data.length;
if (isOpen) {
// 写入数据头
sendData(new byte[] { (b笔记本电脑怎样连wifiyte) 0xA1 });
// 写入数组长度
sendData(intToByteArray(datalen));
//shell怎样读 写入数据值
sendData(data);
}
// 从头翻开预览
camera.stopPandroid/yunosreviewjavaee();
camershellya.startPreview();
}笔记本
});
}
}javaapi中文在线看
}

6)当树面试立联接之后可Shell以向PC端发送数据。
数据的发送需求调用ThreadReadWriterIOSocket的write办法,将图画信息数据发送给PC。

发送办法android下载安装sendData如下:

public void sendData(final b笔记本电脑性价比排行yte[] data) {
threadSocketandroid的drawable类.writeData(data);
}

3.3 PC端建议联接

PC端作为客户端需求向服务端建议联接。PC的端的程序运用Java结束。主函数代码如下:

 publ笔记本电脑开机黑屏没反应怎样办ic class Main {
//用于通讯的socket
private static Socket socket;
//输入输出数据流
private static OutputStream out;
private static InputStream in;
public  static MyFrame frame;
private stat笔记本cpu天梯图ic String CMD = "";
public sshellfishtatic void main(String[] args) {
//创立界面
frame = new MyFrandroid下载安装ame();
//增加联接按钮
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout(FlowLayout面试问题大全及答案大全.CENTER, 10, 0));
final JButto笔记本cpu天梯图n button = new JButton("联接");java言语
panel2.add(button);
button.addandroid竞赛专用包ActionListener(new ActionListener() {
@Override
pushell的意思blic void actionPerfoandroid平板电脑价格rmed(ActionEvent arg0) {
neJavaw Thjava怎样读read() {
public void run(笔记本电脑) {
//点击联接按钮,PC端主面试张联接
onConnect();
};
}.start(shell脚本编程100例);
button.setEnabled(false);
}
});
//创立断开按钮android什么意思
JButton button2 = new JButton("断开");笔记本电脑怎样连wifi
button2.addActionListener(new ActionListener() {
@Oveshell编程rride
public void action笔记本电脑开不了机Performed(ActionEveshell脚本根本指令nt arg0) {
//发shell脚本编程100例送断开指令
CMD = Constant.DISCONNECT;
try {
out.flush();
out.write(CMD.getBytes());
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
button.setEnabled(true);
}
});
panel2.add(button2);
//创立摄影按钮
JButton button3 = new JButton("摄影");
button3.addAshell脚本编程100例ctionListener(new ActionListener() {
@Override
public void actionPerformed(Ashell是什么意思中文ctionEvent arg0) {
//发送摄影指令
CMD = Constant.TAKEPHOTandroid竞赛专用包O;
try {
out.flush();
out.wjava言语rite(CMD.getBytes());
out.flush();
} catch (IOException e) {
e.printStackTrace();javascript
}
}
});
panel2.add(button3);
frame.add(pshell脚本根本指令anel2, BorderLayout.SOUTH);
frame.setResizable(false);
frame.setDefaultCloseOperationjava工作培训班(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
//建议联接时运用adb发送体系播送
protected static void onConnect() {
try {
//adb指令,发送Notiandroid下载安装fyServiceSandroid竞赛专用包top播送,手机接纳到此播送连续监听
Runtime.getRuntime().exec(
"adb shell am broadcast -面试毛遂自荐3分钟通用a NotifyServiceStop");
Thread.sleep(2000);
//调用adb的端口转化指令,将java工作培训班PC端建议的tcp联接指向手机服务器
Runtime.getRuntijavaeeme().exec("adb forward tcp:12580 tcshell的意思p:10086"); // 端口转化
Thread.sleep(2000);
//发送NotifyServiceStart播送,手机接纳到此播送建议监听
Runtime.getRuntime().exec(
"adb shell am broadcast -a NotifyServiceStart");
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
int len = 0;
int degree = 0;
try {android竞赛专用包
//PC端建议TCP联接
InetAddress serveraddr = null;
serveraddr = InetAddress.getByName("127.0.0.1");
System.out.println("TCP 1 " + "正在联接。。。。");
socket = new Socket(serveraddr, 12580);
System.笔记本cpu天梯图out.println("TCP 2 " + "联接成功 ");
// 获取输入输出流
ou笔记本电脑t = new BufferedOutputStream(socket.getOandroid是什么手机牌子utputStream());
in  = new BufferedInputStream(socket.getInputStream());笔记本cpu天梯图
// 发送联接指令
CMShellD = Constant.CONNECT;
out.write(CMD.getBytes());
out.flush();
boolean flag = true;
if (socke笔记本电脑排名前十t.isConnecte面试毛遂自荐一分钟d()) {
while (flagshell脚本编程100例) {
try {
int head = in.read();
if (head == 0xA0) {//获取数据头
byte[] src = new byte[4面试毛遂自荐3分钟通用];
len = in.read(srcshell是什么意思中文);
len = byteArrayToInt(src);//获取数据长java初学度
if (len &g面试问题t; 0) {
int cclen = 0;
byte[] srcshellfishData = new byte[len];
while (cclen < len) {
int readlen = in.read(srcData, cclen, len
- cclen);
cclen +=javascript readlen;
}
//将图画数据转为ImageIcon方针
ImageIcon icon = new ImageIcon(面试毛遂自荐srcData);
if (icon != null) {
//闪现图画
frjava模拟器ame.setIcon(icon, degree);
}
}
}else if (head == 0xA1) {//获取数据头
byteshell的意思[] src = new byte[4];
len = in.read(src);shell脚本
len = byteArrayToInt(src);//获取数据长度
if (len > 0) {
int cclen = 0;
byte[] srcData = new byte[len面试毛遂自荐];
while (cclen < len) {
int readlen = in.read(srcData,shellfish cclen, len
- cclen);
cclen += readlen;
}
//保存图片文件
File file = new File("D:images" + System.cjava难学吗urrentTimeMillis() + ".jpg");
if (!file.exists()) {
fi笔记本le.mkdirs();
}
//存储图片
ImageIcon icon = new ImageIcon(srcData);
Image image笔记本 = icon.getImage();
BufferedImage bi = new BufferedImage(image.getWidth(null),imaandroid下载ge.getHeight(nulandroid什么意思l),BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics(Android);
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
ImageIO.write(bi,"jpg",fishell脚本le);
}
}
} catch (Exception e) {
}
}
}
} catcshell是什么意思中文h (Exception e1) {
System.out.println(" 联接出现失常:联接失利: " + e1.toStrin面试毛遂自荐简略大方g());
}
}
public stJavaatic int byte面试问题大全及答案大全ArrayToInt(byte[] b) {
return b[3]java难学吗 & 0xFF | (b[2] & 0xFF) << 8 | (b[1] & 0xFF) << 16
| (b[0] & 0xFF) << 24;
}
}

MyFrajavascriptme类:

public class MyFrame extends JFrame {
private static final long serialVersionUID =javaapi中文在线看 1L;
public MyPajavaapi中文在线看nel panel;
public MyFrjava言语ame() {
// 默许的窗体称号
this.setTitle("USB联接闪现");
// 取java面试题得面板的实例
pandroid体系anel = new MyPanel();
this.add(pa面试问题大全及答案大全nel);
this.pack(shell的意思);
this.setVisibleshell怎样读(true);
}
//设置图片
public void setIcon(ImageIc笔记本on incon, int drgree) {
panel.setImage(incon, drgree);
}
}

MyPanel类:

 pushellfishblicjava面试题 class MyPanel extends Panel {
private stat笔记i笔记本cpu天梯图c final long serialVersijavaapi中文在线看onUID = 1L;
privandroid竞赛专用包ate Image bufferImage;
private final Image screenIAndroidmage = new BufferedImageshell脚本(800, 600, 2);
private fshell脚本inal Graphics2D screandroid手机enGraphic = (Gra面试必问10大问题答复phics2D) screenImage
.getGraphics();
int degree = 90;
private Image back面试gr笔记本oundImage = screshellyenImage;
public MyPanel() {
// 设定焦点在本窗体
setFjava面试题ocusable(true);
// 设定初始结构时面板巨细,这儿先选用图片的巨细
set面试PreferredSize(new Dimension(800, 600));
// 制作布面试技巧和注意事项景
drawView();
}
public void setImage(ImageIcon icon, int degree) {面试常见问题及答复技巧
//System.out.println("面试问题大全及答案大全设置图片   偏转视点" + degree);
this.degree = dejava难学吗gree;
backgroundImage = icon.getImage();
drawView();
repaint();
}
priv面试问题ate void drawView() {
int width  = gandroid/yunoset面试毛遂自荐3分钟通用Width();
intAndroid height = getHeight();
int x = width / 2 - backgroundImage.getandroid的drawable类Width(null) /2;
int y = height / 2 - bjava初学ackgroundImage.android体系getHeight(null) / 2;
scshell编程reenGraphic.drawImage(backgroundImage, x ,shell的意思 y, null);
}
@Override
public void update(Graphics g) {
if (bufferImage == null) {
bufferImage = this.createImage(this.getWidth(), tandroid下载安装his.getHeight());
}
//bufferImandroid/yunosage = rotateIma面试毛遂自荐3分钟通用ge(bufferimage, degree);
Grshellfishaphics gBu笔记本电脑排名前十ffer = bufferImage面试技巧和注意事项.getGraphics();// 取得图片缓冲区的画笔面试毛遂自荐
if (gBuffer != null){
paint(gBuffer);
}
else{
paint(g);
}
gBuffer.dispose();
g.drawImage(bufferImage, 0, 0, null);
}
//闪现图画
public void paint(Graphics g) {
g.drawImage(screenImage, 0, 0, null面试问题);
}
public static BufferedImage rotateImage(final BuffereshellfishdImage bufferedimage,
final int degree) {
int w = buffer面试常见问题及答复技巧edimjava面试题age.gjava初学etWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel(笔记本).getTransparency();
BufferedImage img;
Grapandroid体系hics2D graphics2d;
(graphics2d =Android (img = new Buandroid平板电脑价格fferedshell是什么意思中文Image(w, h, type))
.crjava初学eateGraphics()).setRenderingHint(
RenderingHintandroid是什么手机牌子s.K笔记本电脑开不了机EY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
graphics2d.shellydrawImage(bufferedimage, 0, 0, null);
graphics2d.disshell的意思pose();
return img;
}
}

两个工程均用到的Constant类

public class Constant {
publiandroid下载c static final String addrIp = "192.16shellfish8.0.Java11android是什么手机牌子9";
public static fishell怎样读nal int addrPort = 56168;
public static final String CONNECT = "java工作培训班CONNECT";
public static final String DISCONNECT = "DISCONNECT";
public static final Sshell编程tring TAKEPHOTO = "TAKEPHOjavaeeTO";
public static final String START = "START";
public static final String STOP = "STOP";
}

本片文章到这儿就结束了,后续会不断更新Android干货,面试题同享希望对我们有所协助。

公众号:程序员喵大人(专注于Android各类学习笔记、面试题以及IT类资讯的同享。)

发表评论

提供最优质的资源集合

立即查看 了解详情