Commit a87a2bec authored by lujunye's avatar lujunye

123123123

parent 159f046a
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
#import "SVProgressHUD.h" #import "SVProgressHUD.h"
#import "BtViewController.h" #import "BtViewController.h"
#import "WKWebView+UIImage.h"
#import "BLEConnecter.h" #import "BLEConnecter.h"
#import "EthernetConnecter.h" #import "EthernetConnecter.h"
......
//
// HLBLEConst.h
// HLBluetoothDemo
//
// Created by Harvey on 16/4/29.
// Copyright © 2016年 Halley. All rights reserved.
//
#ifndef HLBLEConst_h
#define HLBLEConst_h
typedef NS_ENUM(NSInteger, HLOptionStage) {
HLOptionStageConnection, //蓝牙连接阶段
HLOptionStageSeekServices, //搜索服务阶段
HLOptionStageSeekCharacteristics, //搜索特性阶段
HLOptionStageSeekdescriptors, //搜索描述信息阶段
};
#pragma mark ------------------- 通知的定义 --------------------------
/** 蓝牙状态改变的通知 */
#define kCentralManagerStateUpdateNoticiation @"kCentralManagerStateUpdateNoticiation"
#pragma mark ------------------- block的定义 --------------------------
/** 蓝牙状态改变的block */
typedef void(^HLStateUpdateBlock)(CBCentralManager *central);
/** 发现一个蓝牙外设的block */
typedef void(^HLDiscoverPeripheralBlock)(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI);
/** 连接完成的block,失败error就不为nil */
typedef void(^HLConnectCompletionBlock)(CBPeripheral *peripheral, NSError *error);
/** 搜索到连接上的蓝牙外设的服务block */
typedef void(^HLDiscoveredServicesBlock)(CBPeripheral *peripheral, NSArray *services, NSError *error);
/** 搜索某个服务的子服务 的回调 */
typedef void(^HLDiscoveredIncludedServicesBlock)(CBPeripheral *peripheral,CBService *service, NSArray *includedServices, NSError *error);
/** 搜索到某个服务中的特性的block */
typedef void(^HLDiscoverCharacteristicsBlock)(CBPeripheral *peripheral, CBService *service, NSArray *characteristics, NSError *error);
/** 收到某个特性值更新的回调 */
typedef void(^HLNotifyCharacteristicBlock)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error);
/** 查找到某个特性的描述 block */
typedef void(^HLDiscoverDescriptorsBlock)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSArray *descriptors, NSError *error);
/** 统一返回使用的block */
typedef void(^HLBLECompletionBlock)(HLOptionStage stage, CBPeripheral *peripheral,CBService *service, CBCharacteristic *character, NSError *error);
/** 获取特性中的值 */
typedef void(^HLValueForCharacteristicBlock)(CBCharacteristic *characteristic, NSData *value, NSError *error);
/** 获取描述中的值 */
typedef void(^HLValueForDescriptorBlock)(CBDescriptor *descriptor,NSData *data,NSError *error);
/** 往特性中写入数据的回调 */
typedef void(^HLWriteToCharacteristicBlock)(CBCharacteristic *characteristic, NSError *error);
/** 往描述中写入数据的回调 */
typedef void(^HLWriteToDescriptorBlock)(CBDescriptor *descriptor, NSError *error);
/** 获取蓝牙外设信号的回调 */
typedef void(^HLGetRSSIBlock)(CBPeripheral *peripheral,NSNumber *RSSI, NSError *error);
#endif /* HLBLEConst_h */
//
// HLBLEManager.h
// HLBluetoothDemo
//
// Created by Harvey on 16/4/27.
// Copyright © 2016年 Halley. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "HLBLEConst.h"
@interface HLBLEManager : NSObject
#pragma mark - properties
/** 蓝牙模块状态改变的回调 */
@property (copy, nonatomic) HLStateUpdateBlock stateUpdateBlock;
/** 发现一个蓝牙外设的回调 */
@property (copy, nonatomic) HLDiscoverPeripheralBlock discoverPeripheralBlcok;
/** 连接外设完成的回调 */
@property (copy, nonatomic) HLConnectCompletionBlock connectCompleteBlock;
/** 发现服务的回调 */
@property (copy, nonatomic) HLDiscoveredServicesBlock discoverServicesBlock;
/** 发现服务中的特性的回调 */
@property (copy, nonatomic) HLDiscoverCharacteristicsBlock discoverCharacteristicsBlock;
/** 特性值改变的回调 */
@property (copy, nonatomic) HLNotifyCharacteristicBlock notifyCharacteristicBlock;
/** 发现服务中的子服务的回调 */
@property (copy, nonatomic) HLDiscoveredIncludedServicesBlock discoverdIncludedServicesBlock;
/** 发现特性的描述的回调 */
@property (copy, nonatomic) HLDiscoverDescriptorsBlock discoverDescriptorsBlock;
/** 操作完成的统一回调 */
@property (copy, nonatomic) HLBLECompletionBlock completionBlock;
/** 获取特性值回调 */
@property (copy, nonatomic) HLValueForCharacteristicBlock valueForCharacteristicBlock;
/** 获取描述值的回调 */
@property (copy, nonatomic) HLValueForDescriptorBlock valueForDescriptorBlock;
/** 将数据写入特性中的回调 */
@property (copy, nonatomic) HLWriteToCharacteristicBlock writeToCharacteristicBlock;
/** 将数据写入描述中的回调*/
@property (copy, nonatomic) HLWriteToDescriptorBlock writeToDescriptorBlock;
/** 获取蓝牙外设信号强度的回调 */
@property (copy, nonatomic) HLGetRSSIBlock getRSSIBlock;
@property (strong, nonatomic, readonly) CBPeripheral *connectedPerpheral; /**< 当前连接的外设 */
/**
* 每次发送的最大数据长度,因为部分型号的蓝牙打印机一次写入数据过长,会导致打印乱码。
* iOS 9之后,会调用系统的API来获取特性能写入的最大数据长度。
* 但是iOS 9之前需要自己测试然后设置一个合适的值。默认值是146,我使用佳博58MB-III的限度。
* 所以,如果你打印乱码,你考虑将该值设置小一点再试试。
*/
@property (assign, nonatomic) NSInteger limitLength;
#pragma mark - method
+ (instancetype)sharedInstance;
/**
* 开始搜索蓝牙外设,每次在block中返回一个蓝牙外设信息
*
* @param uuids 服务的CBUUID
* @param option 其他可选参数
*/
- (void)scanForPeripheralsWithServiceUUIDs:(NSArray<CBUUID *> *)uuids options:(NSDictionary<NSString *, id> *)options;
/**
* 开始搜索蓝牙外设,每次在block中返回一个蓝牙外设信息
* 返回的block参数可参考CBCentralManager 的 centralManager:didDiscoverPeripheral:advertisementData:RSSI:
*
* @param uuids 服务的CBUUID
* @param option 其他可选参数
* @param discoverBlock 搜索到蓝牙外设后的回调
*/
- (void)scanForPeripheralsWithServiceUUIDs:(NSArray<CBUUID *> *)uuids options:(NSDictionary<NSString *, id> *)options didDiscoverPeripheral:(HLDiscoverPeripheralBlock)discoverBlock;
/**
* 连接某个蓝牙外设,并查询服务,特性,特性描述
*
* @param peripheral 要连接的蓝牙外设
* @param connectOptions 连接的配置参数
* @param stop 连接成功后是否停止搜索蓝牙外设
* @param serviceUUIDs 要搜索的服务UUID
* @param characteristicUUIDs 要搜索的特性UUID
* @param completionBlock 操作执行完的回调
*/
- (void)connectPeripheral:(CBPeripheral *)peripheral
connectOptions:(NSDictionary<NSString *,id> *)connectOptions
stopScanAfterConnected:(BOOL)stop
servicesOptions:(NSArray<CBUUID *> *)serviceUUIDs
characteristicsOptions:(NSArray<CBUUID *> *)characteristicUUIDs
completeBlock:(HLBLECompletionBlock)completionBlock;
/**
* 查找某个服务的子服务
*
* @param includedServiceUUIDs 要查找的子服务的UUIDs
* @param service 父服务
*/
- (void)discoverIncludedServices:(NSArray<CBUUID *> *)includedServiceUUIDs forService:(CBService *)service;
/**
* 读取某个特性的值
*
* @param characteristic 要读取的特性
*/
- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic;
/**
* 读取某个特性的值
*
* @param characteristic 要读取的特性
* @param completionBlock 读取完后的回调
*/
- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic completionBlock:(HLValueForCharacteristicBlock)completionBlock;
/**
* 往某个特性中写入数据
*
* @param data 写入的数据
* @param characteristic 特性对象
* @param type 写入类型
*/
- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type;
/**
* 往某个特性中写入数据
*
* @param data 写入的数据
* @param characteristic 特性对象
* @param type 写入类型
* @param completionBlock 写入完成后的回调,只有type为CBCharacteristicWriteWithResponse时,才会回调
*/
- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type completionBlock:(HLWriteToCharacteristicBlock)completionBlock;
/**
* 读取某特性的描述信息
*
* @param descriptor 描述对象
*/
- (void)readValueForDescriptor:(CBDescriptor *)descriptor;
/**
* 读取某特性的描述信息
*
* @param descriptor 描述对象
* @param completionBlock 读取结果返回时的回调
*/
- (void)readValueForDescriptor:(CBDescriptor *)descriptor completionBlock:(HLValueForDescriptorBlock)completionBlock;
/**
* 将数据写入特性的描述中
*
* @param data 数据
* @param descriptor 描述对象
*/
- (void)writeValue:(NSData *)data forDescriptor:(CBDescriptor *)descriptor;
/**
* 将数据写入特性的描述中
*
* @param data 数据
* @param descriptor 描述对象
* @param completionBlock 数据写入完成后的回调
*/
- (void)writeValue:(NSData *)data forDescriptor:(CBDescriptor *)descriptor completionBlock:(HLWriteToDescriptorBlock)completionBlock;
/**
* 获取某外设的信号
*
* @param completionBlock 获取信号完成后的回调
*/
- (void)readRSSICompletionBlock:(HLGetRSSIBlock)getRSSIBlock;
/**
* 停止扫描
*/
- (void)stopScan;
/**
* 断开蓝牙连接
*/
- (void)cancelPeripheralConnection;
@end
//
// HLPrinter.h
// HLBluetoothDemo
//
// Created by Harvey on 16/5/3.
// Copyright © 2016年 Halley. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "UIImage+Bitmap.h"
typedef NS_ENUM(NSInteger, HLPrinterStyle) {
HLPrinterStyleDefault,
HLPrinterStyleCustom
};
/** 文字对齐方式 */
typedef NS_ENUM(NSInteger, HLTextAlignment) {
HLTextAlignmentLeft = 0x00,
HLTextAlignmentCenter = 0x01,
HLTextAlignmentRight = 0x02
};
/** 字号 */
typedef NS_ENUM(NSInteger, HLFontSize) {
HLFontSizeTitleSmalle = 0x00,
HLFontSizeTitleMiddle = 0x11,
HLFontSizeTitleBig = 0x22
};
@interface HLPrinter : NSObject
/**
* 添加单行标题,默认字号是小号字体
*
* @param title 标题名称
* @param alignment 标题对齐方式
*/
- (void)appendText:(NSString *)text alignment:(HLTextAlignment)alignment;
/**
* 添加单行标题
*
* @param title 标题名称
* @param alignment 标题对齐方式
* @param fontSize 标题字号
*/
- (void)appendText:(NSString *)text alignment:(HLTextAlignment)alignment fontSize:(HLFontSize)fontSize;
/**
* 添加单行信息,左边名称(左对齐),右边实际值(右对齐),默认字号是小号。
* @param title 名称
* @param value 实际值
* 警告:因字号和字体与iOS中字体不一致,计算出来有误差,可以用[-appendTitle:value:valueOffset:]或[-appendTitle:value:valueOffset:fontSize:]
*/
- (void)appendTitle:(NSString *)title value:(NSString *)value;
/**
* 添加单行信息,左边名称(左对齐),右边实际值(右对齐)。
* @param title 名称
* @param value 实际值
* @param fontSize 字号大小
* 警告:因字号和字体与iOS中字体不一致,计算出来有误差,所以建议用在价格方面
*/
- (void)appendTitle:(NSString *)title value:(NSString *)value fontSize:(HLFontSize)fontSize;
/**
* 设置单行信息,左标题,右实际值
* @提醒 该方法的预览效果与实际效果误差较大,请以实际打印小票为准
*
* @param title 标题
* @param value 实际值
* @param offset 实际值偏移量
*/
- (void)appendTitle:(NSString *)title value:(NSString *)value valueOffset:(NSInteger)offset;
/**
* 设置单行信息,左标题,右实际值
* @提醒 该方法的预览效果与实际效果误差较大,请以实际打印小票为准
*
* @param title 标题
* @param value 实际值
* @param offset 实际值偏移量
* @param fontSize 字号
*/
- (void)appendTitle:(NSString *)title value:(NSString *)value valueOffset:(NSInteger)offset fontSize:(HLFontSize)fontSize;
/**
* 添加选购商品信息标题,一般是三列,名称、数量、单价
*
* @param LeftText 左标题
* @param middleText 中间标题
* @param rightText 右标题
*/
- (void)appendLeftText:(NSString *)left middleText:(NSString *)middle rightText:(NSString *)right isTitle:(BOOL)isTitle;
/**
* 添加图片,一般是添加二维码或者条形码
* ⚠️提醒:这种打印图片的方式,是自己生成图片,然后用位图打印
*
* @param image 图片
* @param alignment 图片对齐方式
* @param maxWidth 图片的最大宽度,如果图片过大,会等比缩放
*/
- (void)appendImage:(UIImage *)image alignment:(HLTextAlignment)alignment maxWidth:(CGFloat)maxWidth;
/**
* 添加条形码图片
* ⚠️提醒:这种打印条形码的方式,是自己生成条形码图片,然后用位图打印图片
*
* @param info 条形码中包含的信息,默认居中显示,最大宽度为300。如果大于300,会等比缩放。
*/
- (void)appendBarCodeWithInfo:(NSString *)info;
/**
* 添加条形码图片
* ⚠️提醒:这种打印条形码的方式,是自己生成条形码图片,然后用位图打印图片
*
* @param info 条形码中的信息
* @param alignment 图片对齐方式
* @param maxWidth 图片最大宽度
*/
- (void)appendBarCodeWithInfo:(NSString *)info alignment:(HLTextAlignment)alignment maxWidth:(CGFloat)maxWidth;
/**
* 添加二维码
* ✅推荐:这种方式使用的是打印机的指令生成二维码并打印机,所以比较推荐这种方式
*
* @param info 二维码中的信息
* @param size 二维码的大小 取值范围1 <= size <= 16
*/
- (void)appendQRCodeWithInfo:(NSString *)info size:(NSInteger)size;
/**
* 添加二维码
* ✅推荐:这种方式使用的是打印机的指令生成二维码并打印机,所以比较推荐这种方式
*
* @param info 二维码中的信息
* @param size 二维码大小,取值范围 1 <= size <= 16
* @param alignment 设置图片对齐方式
*/
- (void)appendQRCodeWithInfo:(NSString *)info size:(NSInteger)size alignment:(HLTextAlignment)alignment;
/**
* 添加二维码图片
* ⚠️提醒:这种打印条二维码的方式,是自己生成二维码图片,然后用位图打印图片
*
* @param info 二维码中的信息
*/
- (void)appendQRCodeWithInfo:(NSString *)info;
/**
* 添加二维码图片
* ⚠️提醒:这种打印条二维码的方式,是自己生成二维码图片,然后用位图打印图片
*
* @param info 二维码中的信息
* @param centerImage 二维码中间的图片
* @param alignment 对齐方式
* @param maxWidth 二维码的最大宽度
*/
- (void)appendQRCodeWithInfo:(NSString *)info centerImage:(UIImage *)centerImage alignment:(HLTextAlignment)alignment maxWidth:(CGFloat )maxWidth;
/**
* 添加一条分割线,like this:---------------------------
*/
- (void)appendSeperatorLine;
/**
* 添加底部信息
*
* @param footerInfo 不填默认为 谢谢惠顾,欢迎下次光临!
*/
- (void)appendFooter:(NSString *)footerInfo;
/**
添加自定义的data
@param data 自定义的data
*/
- (void)appendCustomData:(NSData *)data;
/**
* 获取最终的data
*
* @return 最终的data
*/
- (NSData *)getFinalData;
@end
//
// UIImage+Bitmap.h
// HLBluetoothDemo
//
// Created by Harvey on 16/5/3.
// Copyright © 2016年 Halley. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger,BitPixels) {
BPAlpha = 0,
BPBlue = 1,
BPGreen = 2,
BPRed = 3
};
@interface UIImage (Bitmap)
/**
* 将图片转换为点阵图数据
*
* @return 转化后的点阵图数据
*/
- (NSData *)bitmapData;
/**
* 将图片绘制到绘图上下文中,并返回上下文
*
* @return
*/
//+ (CGContextRef)bitmapRGBA8ContextFromImage:(CGImageRef)image;
- (CGContextRef)bitmapRGBA8Context;
/**
* 缩放图片,会按照给定的最大宽度,等比缩放
*
* @param maxWidth 缩放后的最大宽度
*
* @return 返回缩放后的图片
*/
- (UIImage *)imageWithscaleMaxWidth:(CGFloat)maxWidth;
/**
* 将图片转换为黑白图片
*
* @return 黑白图片
*/
- (UIImage *)blackAndWhiteImage;
@end
#pragma mark - -----------制作二维码 条形码------------
@interface UIImage (QRCode)
/**
* 创建条形码
*
* @param info 字符串信息
*
* @return 条形码图片
*/
+ (UIImage *)barCodeImageWithInfo:(NSString *)info;
/**
* 创建二维码
*
* @param info 二维码内的信息
* @param image 二维码中心的logo图片
* @param width 二维码的宽度
*
* @return 二维码图片
*/
+ (UIImage *)qrCodeImageWithInfo:(NSString *)info centerImage:(UIImage *)image width:(CGFloat)width;
/**
* 将CIImage 放大显示,并转换为UIImage。
*
* @param image 原始CIImage
* @param size 最终尺寸的宽度
*
* @return UIImage
*/
+ (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat)size;
/**
* 将原图转变为背景色透明,图片为设置的颜色
*
* @param image 要改变的图片
* @param red red
* @param green green
* @param blue blue
*
* @return 返回修改后的图片
*/
+ (UIImage*)imageBgColorToTransparentWith:(UIImage*)image withRed:(CGFloat)red andGreen:(CGFloat)green andBlue:(CGFloat)blue;
@end
//
// HLBluetoothDemo
//
// Created by Harvey on 16/5/13.
// Copyright © 2016年 Halley. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>
@interface WKWebView (UIImage)
/**
* 获取当前加载的网页的截图
*
* @return
*/
- (UIImage *)imageForWebView;
@end
//
// HLBluetoothDemo
//
// Created by Harvey on 16/5/13.
// Copyright © 2016年 Halley. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "WKWebView+UIImage.h"
@implementation WKWebView (UIImage)
/**
* 获取当前加载的网页的截图
* 获取当前WebView的size,然后一屏一屏的截图后,再拼接成一张完整的图片
*
* @return
*/
- (UIImage *)imageForWebView
{
// 1.获取WebView的宽高
CGSize boundsSize = self.bounds.size;
CGFloat boundsWidth = boundsSize.width;
CGFloat boundsHeight = boundsSize.height;
// 2.获取contentSize
CGSize contentSize = self.scrollView.contentSize;
CGFloat contentHeight = contentSize.height;
// 3.保存原始偏移量,便于截图后复位
CGPoint offset = self.scrollView.contentOffset;
// 4.设置最初的偏移量为(0,0);
[self.scrollView setContentOffset:CGPointMake(0, 0)];
NSMutableArray *images = [NSMutableArray array];
while (contentHeight > 0) {
// 5.获取CGContext 5.获取CGContext
UIGraphicsBeginImageContextWithOptions(boundsSize, NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 6.渲染要截取的区域
[self.layer renderInContext:ctx];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// 7.截取的图片保存起来
[images addObject:image];
CGFloat offsetY = self.scrollView.contentOffset.y;
[self.scrollView setContentOffset:CGPointMake(0, offsetY + boundsHeight)];
contentHeight -= boundsHeight;
}
// 8 webView 恢复到之前的显示区域
[self.scrollView setContentOffset:offset];
CGFloat scale = [UIScreen mainScreen].scale;
CGSize imageSize = CGSizeMake(contentSize.width * scale,
contentSize.height * scale);
// 9.根据设备的分辨率重新绘制、拼接成完整清晰图片
UIGraphicsBeginImageContext(imageSize);
[images enumerateObjectsUsingBlock:^(UIImage *image, NSUInteger idx, BOOL *stop) {
[image drawInRect:CGRectMake(0,
scale * boundsHeight * idx,
scale * boundsWidth,
scale * boundsHeight)];
}];
UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return fullImage;
}
@end
...@@ -7,10 +7,7 @@ ...@@ -7,10 +7,7 @@
// //
#import "BtViewController.h" #import "BtViewController.h"
#import "HLBLEManager.h"
#import "SVProgressHUD.h" #import "SVProgressHUD.h"
#import "HLPrinter.h"
#import "WKWebView+UIImage.h"
#import "Bt_Cell.h" #import "Bt_Cell.h"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment