Java Code Examples for cn.hutool.log.StaticLog#error()

The following examples show how to use cn.hutool.log.StaticLog#error() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommUtils.java    From tools-ocr with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static byte[] imageToBytes(BufferedImage img) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    MemoryCacheImageOutputStream outputStream = new MemoryCacheImageOutputStream(byteArrayOutputStream);
    try {
        Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = (ImageWriter) iter.next();
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(IMAGE_QUALITY);
        writer.setOutput(outputStream);
        IIOImage image = new IIOImage(img, null, null);
        writer.write(null, image, iwp);
        writer.dispose();
        byte[] result = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.close();
        outputStream.close();
        return result;
    } catch (IOException e) {
        StaticLog.error(e);
        return new byte[0];
    }
}
 
Example 2
Source File: CommUtils.java    From tools-ocr with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static String postMultiData(String url, byte[] data, String boundary, String cookie, String referer) {
    try {
        HttpRequest request = HttpUtil.createPost(url).timeout(15000);
        request.contentType("multipart/form-data; boundary=" + boundary);
        request.body(data);
        if (StrUtil.isNotBlank(referer)) {
            request.header("Referer", referer);
        }
        if (StrUtil.isNotBlank(cookie)) {
            request.cookie(cookie);
        }
        HttpResponse response = request.execute();
        return WebUtils.getSafeHtml(response);
    } catch (Exception ex) {
        StaticLog.error(ex);
        return null;
    }
}
 
Example 3
Source File: CommUtils.java    From tools-ocr with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void initStage(Stage stage) {
    try {
        if (CommUtils.IS_MAC_OS) {
            URL iconURL = MainFm.class.getResource("/img/logo.png");
            java.awt.Image image = new ImageIcon(iconURL).getImage();
            Class appleApp = Class.forName("com.apple.eawt.Application");
            //noinspection unchecked
            Method getApplication = appleApp.getMethod("getApplication");
            Object application = getApplication.invoke(appleApp);
            Class[] params = new Class[1];
            params[0] = java.awt.Image.class;
            //noinspection unchecked
            Method setDockIconImage = appleApp.getMethod("setDockIconImage", params);
            setDockIconImage.invoke(application, image);
        }
    } catch (Exception e) {
        StaticLog.error(e);
    }
    stage.setTitle("树洞OCR文字识别");
    stage.getIcons().add(new javafx.scene.image.Image(MainFm.class.getResource("/img/logo.png").toExternalForm()));
}
 
Example 4
Source File: MainFm.java    From tools-ocr with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static void recImage() {
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Please Select Image File");
    fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg"));
    File selectedFile = fileChooser.showOpenDialog(stage);
    if (selectedFile == null || !selectedFile.isFile()) {
        return;
    }
    stageInfo = new StageInfo(stage.getX(), stage.getY(),
            stage.getWidth(), stage.getHeight(), stage.isFullScreen());
    MainFm.stage.close();
    try {
        BufferedImage image = ImageIO.read(selectedFile);
        doOcr(image);
    } catch (IOException e) {
        StaticLog.error(e);
    }
}
 
Example 5
Source File: GlobalKeyListener.java    From tools-ocr with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void preventEvent(NativeKeyEvent e){
    try {
        Field f = NativeInputEvent.class.getDeclaredField("reserved");
        f.setAccessible(true);
        f.setShort(e, (short) 0x01);
    }
    catch (Exception ex) {
        StaticLog.error(ex);
    }
}
 
Example 6
Source File: ScreenCapture.java    From tools-ocr with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void prepareImage() {
	gc.clearRect(0, 0, stage.getWidth(), stage.getHeight());
	BufferedImage image;
	try {
		mainCanvas.setDisable(true);
		image = new Robot().createScreenCapture(new Rectangle(data.rectUpperLeftX + CaptureInfo.ScreenMinX, data.rectUpperLeftY, data.rectWidth, data.rectHeight));
	} catch (AWTException ex) {
		StaticLog.error(ex);
		return;
	} finally {
		mainCanvas.setDisable(false);
		MainFm.restore(false);
	}
	MainFm.doOcr(image);
}
 
Example 7
Source File: DocumentBrowser.java    From book118-downloader with MIT License 5 votes vote down vote up
private String moveToNextPage(PdfInfo pInfo) {
    String sNextPage = getNextPage(pInfo);
    try {
        pInfo.setImg(URLEncoder.encode(sNextPage, "utf8"));
    } catch (Exception e) {
        StaticLog.error("moveToNextPage error", e);
    }
    return sNextPage;
}
 
Example 8
Source File: DocumentBrowser.java    From book118-downloader with MIT License 5 votes vote down vote up
/**
 * 下载文件到本地
 *
 * @param url       文件的 url
 * @param localPath 本地存储路径
 */
private void downloadFile(String url, String localPath) {
    File file = new File(localPath);
    try (FileOutputStream fileOutputStream = new FileOutputStream(file); BufferedOutputStream out = new BufferedOutputStream(fileOutputStream)) {
        HttpUtil.download(url, out, true);
    } catch (IOException e) {
        StaticLog.error(e.getMessage());
    }
}
 
Example 9
Source File: WebUtils.java    From tools-ocr with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static HttpResponse get(String url, int userAgent, Map<String, String> headers, boolean allowRedirct) {
    try {
        HttpRequest request = HttpUtil.createGet(url).timeout(10000).setFollowRedirects(allowRedirct);
        if (headers == null) {
            headers = new Hashtable<>();
        }
        switch (userAgent) {
            case 1:
                headers.put("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13F69 MicroMessenger/6.3.16 NetType/WIFI Language/zh_CN");
                break;
            case 2:
                headers.put("User-Agent", "Mozilla/5.0 (Linux; U; Android 2.2; en-gb; GT-P1000 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");
                break;
            case 3:
                headers.put("User-Agent", "Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; NOKIA; Lumia 930) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Mobile Safari/537.36 Edge/13.10586");
                break;
            case 4:
                headers.put("User-Agent", "NativeHost");
                break;
            case 5:
                headers.put("User-Agent", "Dalvik/1.6.0 (Linux; U; Android 4.4.2; NoxW Build/KOT49H) ITV_5.7.1.46583");
                break;
            case 6:
                headers.put("User-Agent", "qqlive");
                break;
            case 7:
                headers.put("User-Agent", "Dalvik/1.6.0 (Linux; U; Android 4.2.2; 6S Build/JDQ39E)");
                break;
            case 8:
                headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) XIAMI-MUSIC/3.0.9 Chrome/56.0.2924.87 Electron/1.6.11 Safari/537.36");
                break;
            case 9:
                headers.put("User-Agent", "okhttp/2.7.5");
                break;
            case 10:
                headers.put("User-Agent", "Mozilla/5.0 (Linux; Android 5.1.1; oppo r11 plus Build/LMY48Z) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Mobile Safari/537.36 SogouSearch Android1.0 version3.0");
                break;
            default:
                headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
                break;
        }
        request.addHeaders(headers);
        return request.execute();
    } catch (Exception ex) {
        StaticLog.error(ex);
        return null;
    }
}
 
Example 10
Source File: WebUtils.java    From tools-ocr with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static HttpResponse postData(String url, Map<String, Object> data, int contentType, int userAgent, Map<String, String> headers) {
    try {
        HttpRequest request = HttpUtil.createPost(url).timeout(10000);
        if (contentType == 0) {
            request.contentType("application/x-www-form-urlencoded");
            request.form(data);
        } else if (contentType == 1) {
            request.body(data.values().iterator().next().toString(), "application/json;charset=UTF-8");
        } else {
            request.contentType("application/x-www-form-urlencoded");
            request.body(data.values().iterator().next().toString());
        }
        if (headers == null) {
            headers = new Hashtable<>();
        }
        switch (userAgent) {
            case 1:
                headers.put("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3");
                break;
            case 2:
                headers.put("User-Agent", "Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19");
                break;
            case 3:
                headers.put("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 920)");
                break;
            case 4:
                headers.put("User-Agent", "NativeHost");
                break;
            case 5:
                headers.put("User-Agent", "Apache-HttpClient/UNAVAILABLE (java 1.4)");
                break;
            case 6:
                headers.put("User-Agent", "Mozilla/5.0 (iPad; CPU OS 8_1_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B466 Safari/600.1.4");
                break;
            case 7:
                headers.put("User-Agent", "okhttp/2.7.5");
                break;
            case 10:
                headers.put("User-Agent", "Mozilla/5.0 (Linux; Android 5.1.1; oppo r11 plus Build/LMY48Z) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Mobile Safari/537.36 SogouSearch Android1.0 version3.0");
                break;
            default:
                headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
                break;
        }
        request.addHeaders(headers);
        return request.execute();
    } catch (Exception ex) {
        StaticLog.error(ex);
        return null;
    }
}
 
Example 11
Source File: DocumentBrowser.java    From book118-downloader with MIT License 4 votes vote down vote up
/**
 * 获取文档的预览地址
 *
 * @param documentId 文档的编号
 * @return 预览地址
 */
private PdfInfo getPdfInfo(String documentId) {
    String url = Constants.OPEN_FULL_URL + documentId;
    String pdfPageUrlStr = HttpUtil.get(url);

    if (StrUtil.isNotBlank(pdfPageUrlStr)) {
        pdfPageUrlStr = "https:" + pdfPageUrlStr;
    } else {
        StaticLog.error("获取失败!");
        return null;
    }

    int endOfHost = pdfPageUrlStr.indexOf("?");
    String viewHost = pdfPageUrlStr.substring(0, endOfHost);
    String redirectPage = HttpUtil.get(pdfPageUrlStr);
    String href = ReUtil.get(Constants.HREF_PATTERN, redirectPage, 1);
    String fullUrl;
    if(href != null){
        fullUrl = viewHost.substring(0, viewHost.length()-1) + HtmlUtil.unescape(href);
    }else {
        fullUrl = pdfPageUrlStr;
    }


    String pdfPageHtml = HttpUtil.get(fullUrl);
    if (pdfPageHtml.contains(Constants.FILE_NOT_EXIST)) {
        StaticLog.error("获取预览地址失败,请稍后再试!");
        return null;
    }

    List<String> result = ReUtil.findAllGroup0(Constants.INPUT_PATTERN, pdfPageHtml);
    Map<String, String> pdfInfoMap = new HashMap<>(6);
    pdfInfoMap.put("host", viewHost);
    for (String inputStr : result) {
        String id = ReUtil.get(Constants.ID_PATTERN, inputStr, 1);
        String value = ReUtil.get(Constants.VALUE_PATTERN, inputStr, 1);
        if (StrUtil.isNotBlank(id) && StrUtil.isNotBlank(value)) {
            id = id.toLowerCase();
            try {
                value = URLEncoder.encode(value, "utf8");
            } catch (Exception e) {
                StaticLog.error("URLEncoder Error", e);
            }
            pdfInfoMap.put(id, value);
        }
    }
    return BeanUtil.mapToBean(pdfInfoMap, PdfInfo.class, true);
}