android无线内网打印机打印出图片
(2)手机:手机必须连接wifi才行。
一个按钮,点击此按钮进行打印操作:
button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {PrintUtil printUtil = new PrintUtil(context,path); //context:上下文对象 path:需要打印的图片的路径}});
PrintUtil:
public class PrintUtil {public static String IPADDRESS;public Socket socket;public static final int POS_OPEN_NETPORT = 9100;public static String IPADRESS;public int Net_ReceiveTimeout = 2500;Context context;String path;public PrintUtil(Context context,String path) {super();this.context = context;this.path = path;IPADDRESS = getIPAddress(context);RunThread run = new RunThread();run.start();}/** 获取当前手机所连wifi的ip地址*/public static String getIPAddress(Context context) {NetworkInfo info = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();if (info != null && info.isConnected()) {if (info.getType() == ConnectivityManager.TYPE_MOBILE) {// 当前使用2G/3G/4G网络try {for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {NetworkInterface intf = en.nextElement();for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {InetAddress inetAddress = enumIpAddr.nextElement();if (!inetAddress.isLoopbackAddress()&& inetAddress instanceof Inet4Address) {return inetAddress.getHostAddress();}}}} catch (SocketException e) {e.printStackTrace();}} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {// 当前使用无线网络WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);WifiInfo wifiInfo = wifiManager.getConnectionInfo();String ipAddress = intIP2StringIP(wifiInfo.getIpAddress());// 得到IPV4地址return ipAddress;}} else {// 当前无网络连接,请在设置中打开网络}return null;}/*** 将得到的int类型的IP转换为String类型,截取ip的前三段** @param ip* @return*/public static String intIP2StringIP(int ip) {return (ip & 0xFF) + "." + ((ip >> 8) & 0xFF) + "."+ ((ip >> 16) & 0xFF) + ".";}ScheduledExecutorService pool;boolean isFinish; long lastFinishTime = 0;boolean isHave = false;private class RunThread extends Thread{public RunThread(){}@Overridepublic void run() {if(pool==null){pool = Executors.newScheduledThreadPool(100);}for (int i = 2; i < 255; ++i) { //0~255循环找到打印机ipfinal int index = i;pool.execute(new Runnable() {@Overridepublic void run() {try {String ipaddress = IPADDRESS + index;NetPrinter printer = new NetPrinter();printer.Open(ipaddress, NetPrinter.POS_OPEN_NETPORT);if (printer.IFOpen) { //找到打印机isHave = true;pos(ipaddress);handler.sendEmptyMessage(0);return;} else{if(index == 254 && isHave == false){handler.sendEmptyMessage(1);}}} catch (Exception e) {e.printStackTrace();}} }); }}}private Handler handler = new Handler(){public void handleMessage(Message msg) {switch (msg.what) {case 0:Toast.makeText(context, "打印完成!", Toast.LENGTH_SHORT).show();break;case 1:Toast.makeText(context, "未找到打印机,请确认打印机在同一局域网内,谢谢!", Toast.LENGTH_SHORT).show();break;default:break;}};};private void pos(final String ip) {// 开启一个子线程new Thread() {public void run() {try { // 打印机网口IP 打印机端口号 编码方式printImage(ip);} catch (UnknownHostException e) {e.printStackTrace();Log.d("tag", "错误信息1:" + e.toString());} catch (IOException e) {e.printStackTrace();Log.d("tag", "错误信息2:" + e.toString());}}}.start();}//根据IP地址使用打印机打印图片public void printImage(String ip) throws IOException {DataOutputStream outToServer = null;Socket clientSocket;FileInputStream fileInputStream;try {fileInputStream = new FileInputStream(path);InputStream is = fileInputStream;clientSocket = new Socket(ip, 9100);outToServer = new DataOutputStream(clientSocket.getOutputStream());byte[] buffer = new byte[3000];while (is.read(buffer) != -1) {outToServer.write(buffer); }outToServer.flush();} catch (ConnectException connectException) { Log.e("msg", connectException.toString(), connectException);} catch (UnknownHostException e1) {Log.e("msg", e1.toString(), e1);} catch (IOException e1) {Log.e("msg", e1.toString(), e1);} finally {outToServer.close(); }}public void deleteAll(File file) {if (file.isFile() || file.list().length == 0) {file.delete();} else {for (File f : file.listFiles()) {deleteAll(f); // 递归删除每一个文件}file.delete(); // 删除文件夹}}
}
NetPrinter:
public class NetPrinter {public Socket socket;private static final String DEBUG_TAG="NetPrinter";public static final int POS_OPEN_NETPORT = 9100;public boolean IFOpen = false;public int Net_ReceiveTimeout = 2500;//网络打印机 打开网络打印机public boolean Open(String ipaddress, int netport) {if (socket == null) {try {SocketAddress ipe = new InetSocketAddress(Inet4Address.getByName(ipaddress) ,netport);Log.e(DEBUG_TAG, Inet4Address.getByName(ipaddress)+"|"+netport);socket = new Socket();socket.connect(ipe, Net_ReceiveTimeout); //2500ms连接时间,连接不上返回falseIFOpen = true;} catch(Exception e) {IFOpen = false;}} else {try {socket.close();SocketAddress ipe = new InetSocketAddress(Inet4Address.getByName(ipaddress),netport);Log.e(DEBUG_TAG, Inet4Address.getByName(ipaddress)+"|"+netport);socket = new Socket();socket.connect(ipe,Net_ReceiveTimeout);IFOpen = true;} catch(Exception e) {IFOpen = false;}}if(IFOpen){Close();}return IFOpen;}//网络打印机 关闭public void Close() {try {socket.close();socket = null;} catch(Exception e) {e.printStackTrace();}}
}
以上代码就能轻松实现图片的打印,这里我考虑的仅仅只是一台打印机的情况,我这里也只有一台,哈哈。
有什么不足之处请大家指教一下。
android无线内网打印机打印出图片相关推荐
- 【Android】Android 集成商米内置打印机打印票据
文章目录 [Android]Android 集成商米内置打印机打印票据 1.集成商米打印依赖 2.规范接口接口 3.使用到的相关对象以及工具类 4.MainActivity初始化接口 5.Uniapp ...
- python调用打印机打印图片_pyqt5 调用打印机 打印远程图片
求助.... pyqt5 实现 打印机 打印png图片 可以预览 def localPrinter(): printer = QPrinter(QPrinter.HighResolution) # / ...
- 惠普HP LaserJet Pro 400-M401d 打印机打印出的纸出现雨水滴在玻璃的现象!!
今天总装车间六线一台打印合格证的打印机打印出类似雨水滴在玻璃的画面,排除了一些原因,终于解决了,原来是硒鼓漏粉导致,整体更换了一个墨盒就好了.
- 3D打印机内网联机打印(1)orangpiPlus安装Octoprint
硬件 orangpi plus 32GB bano class10 Joy联名卡 固件 Armbian Bullseye mainline based kernel 5.10.y 安装OctoPrin ...
- android 蓝牙打印兼容,在Android中使用蓝牙打印机打印不起作用
我正在开发应用程序,允许用户创建PDF并允许使用蓝牙打印机进行打印. 我创建了PDF,但每当我要使用蓝牙打印机集成打印功能时,都会出现错误. 我无法获得蓝牙设备列表. 如果您有任何示例代码,请提供给我 ...
- android 访问内网ip_android 获取局域网IP与MAC 地址 毫秒级(详解)
两个 linux命令 开始理清思路:主要使用 linux 两个命令(2017-07-21 fix:这个思路不好,后面使用udp取代[毫秒级]) [ping]: 查询某个ip是否被设备占用 [cat] ...
- 小票打印机打印出空白
web打印小票,驱动安装正确,打印空白,这时需要在浏览器页面设置里面设置去掉页面页脚. 转载于:https://www.cnblogs.com/brucetie/p/4747984.html
- 蓝牙连接打印机打印文字图片条形码二维码 用的是Gprinter打印机
实现了蓝牙打印,正在进一步完善,欢迎留言交流 :) Github链接
- 外网访问内网打印机(服务器访问本地打印机)
两种实现方案 第一种 花生壳. 第二种 frp. 优缺点: 花生壳:只需要在本地启动花生壳:不占用服务器带宽.收费,有数量限制. frp:需要在服务器启动服务端,本地启动客户端,占用服务器带宽:免费, ...
最新文章
- 一文详解实时稀疏点云分割
- 苹果内购和 Apple Pay
- mysql中数据表如何关联_mysql 如何导入/导出2个关联表中的数据
- Web应用里的HTTP参数污染(HPP漏洞)
- linux防火墙添加端口并开闭防火墙
- linux - python - 异常:error while loading shared libraries
- C++中的RAII机制
- 设置Web页面试用手机浏览器(iphone)
- 你是我一首唱不完的歌
- Mac使用终端压缩加密文件
- 打家劫舍(Leetcode)动态规划c语言
- 【转】VB动态拖曳ListBox Item位置
- ISO9000标准简介
- 《Option Volatility Pricing》阅读笔记之 Volatility(波动率)
- android 邮箱注册功能,手机邮箱怎么注册登录(安卓手机邮箱设置教程)
- CDMA HERT平台分布式基站 DBS3900 CDMA
- win11 任务栏角溢出里的程序图标记录如何删除(WIN 缓存图标清理)
- Ubuntu系统自动锁屏怎么办
- 项目之: JMSamp、ActiveMQ
- 2022,烟火如常,布衣剩饭,啥也没干,年终总结,蹈海难酬