5 changed files with 135 additions and 0 deletions
@ -0,0 +1,56 @@ |
|||||
|
package com.ccsens.ptccsens.api; |
||||
|
|
||||
|
import cn.hutool.core.codec.Base64; |
||||
|
import cn.hutool.core.util.ImageUtil; |
||||
|
import com.ccsens.cloudutil.annotation.MustLogin; |
||||
|
import com.ccsens.ptccsens.bean.dto.ProjectFinanceDto; |
||||
|
import com.ccsens.ptccsens.bean.vo.OcrVo; |
||||
|
import com.ccsens.ptccsens.bean.vo.ProjectFinanceVo; |
||||
|
import com.ccsens.ptccsens.service.IOcrService; |
||||
|
import com.ccsens.ptccsens.util.BasicsConstant; |
||||
|
import com.ccsens.util.JsonResponse; |
||||
|
import com.ccsens.util.baidu.BaiDuDto; |
||||
|
import com.ccsens.util.baidu.BaiDuUtil; |
||||
|
import com.ccsens.util.baidu.BaiDuVo; |
||||
|
import com.ccsens.util.bean.dto.QueryDto; |
||||
|
import com.github.pagehelper.PageInfo; |
||||
|
import io.swagger.annotations.Api; |
||||
|
import io.swagger.annotations.ApiOperation; |
||||
|
import io.swagger.annotations.ApiParam; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.validation.annotation.Validated; |
||||
|
import org.springframework.web.bind.annotation.*; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import javax.imageio.ImageIO; |
||||
|
import javax.servlet.http.Part; |
||||
|
import java.io.ByteArrayOutputStream; |
||||
|
import java.io.IOException; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* @author AUSU |
||||
|
*/ |
||||
|
@Api(tags = "图片识别" ) |
||||
|
@RestController |
||||
|
@RequestMapping("/ocr") |
||||
|
@Slf4j |
||||
|
public class OcrController { |
||||
|
|
||||
|
@Resource |
||||
|
private IOcrService ocrService; |
||||
|
|
||||
|
@ApiOperation(value = "图像识别", notes = "whj") |
||||
|
@RequestMapping(value = "/bill", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) |
||||
|
public JsonResponse<OcrVo.BillInfo> identifyBill(@RequestParam(required = true) Part part) throws IOException { |
||||
|
log.info("图像识别"); |
||||
|
// 压缩图像
|
||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
||||
|
ImageUtil.scale(ImageIO.read(part.getInputStream()), out, 1f); |
||||
|
String img = Base64.encode(out.toByteArray()); |
||||
|
OcrVo.BillInfo billInfo = ocrService.identifyBill(img); |
||||
|
log.info("图像识别结束:{}", billInfo); |
||||
|
return JsonResponse.newInstance().ok(billInfo); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
package com.ccsens.ptccsens.bean.vo; |
||||
|
|
||||
|
import com.alibaba.fastjson.annotation.JSONField; |
||||
|
import io.swagger.annotations.ApiModel; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class OcrVo { |
||||
|
|
||||
|
@Data |
||||
|
@ApiModel("发票信息") |
||||
|
public static class BillInfo { |
||||
|
@JSONField(name = "InvoiceCode") |
||||
|
@ApiModelProperty("发票代码") |
||||
|
private String invoiceCode; |
||||
|
@JSONField(name = "InvoiceNum") |
||||
|
@ApiModelProperty("发票号码") |
||||
|
private String invoiceNumber; |
||||
|
@JSONField(name = "CommodityAmount") |
||||
|
@ApiModelProperty("金额") |
||||
|
private Long money; |
||||
|
@JSONField(name = "CommodityTax") |
||||
|
@ApiModelProperty("税额") |
||||
|
private Long taxMoney; |
||||
|
@JSONField(name = "InvoiceDate") |
||||
|
@ApiModelProperty("开票时间") |
||||
|
private String invoiceTime; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
package com.ccsens.ptccsens.service; |
||||
|
|
||||
|
import com.ccsens.ptccsens.bean.vo.OcrVo; |
||||
|
|
||||
|
public interface IOcrService { |
||||
|
/** |
||||
|
* 发票识别 |
||||
|
* @param img 发表base64 |
||||
|
* @return 识别结果 |
||||
|
*/ |
||||
|
OcrVo.BillInfo identifyBill(String img); |
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
package com.ccsens.ptccsens.service; |
||||
|
|
||||
|
import com.ccsens.ptccsens.bean.vo.OcrVo; |
||||
|
import com.ccsens.ptccsens.util.BasicsConstant; |
||||
|
import com.ccsens.util.baidu.BaiDuDto; |
||||
|
import com.ccsens.util.baidu.BaiDuUtil; |
||||
|
import com.ccsens.util.baidu.BaiDuVo; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.transaction.annotation.Propagation; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
@Service |
||||
|
@Slf4j |
||||
|
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) |
||||
|
public class OcrService implements IOcrService{ |
||||
|
|
||||
|
@Override |
||||
|
public OcrVo.BillInfo identifyBill(String img) { |
||||
|
// 图像识别
|
||||
|
BaiDuDto.GeneralBasic basic = new BaiDuDto.GeneralBasic(); |
||||
|
basic.setImage(img); |
||||
|
BaiDuVo.BillBasic words = BaiDuUtil.billBasic(BasicsConstant.BaiDu.APP_KEY, BasicsConstant.BaiDu.SECRET_KEY, basic); |
||||
|
log.info("识别结果:{}",words); |
||||
|
// 返回数据
|
||||
|
OcrVo.BillInfo personMsg = new OcrVo.BillInfo(); |
||||
|
// personMsg.toMsg(words.getWordsResult());
|
||||
|
return personMsg; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue