Graphics画图

. java原生画图工具Graphics模拟在题卡上画分数



 /**
     * 上传修改后的图片 
     * 
     * @param ossUrl     图片原来的地址
     * @param request
     * @param modelMap
* @param editFlag  是否画图标识
* @param imageData  base64码
* @param drawjsonArray  分数位置坐标
     * @return
     * @version v1.0
     * @author mayong 
     */
    public String uploadDecideImage(Long decideDetailID, String ossUrl,
            String imageData, Integer editFlag, HttpServletRequest request,
            String drawjsonArray) {
        //获取存放图片的路径
        if (editFlag == null) {
            editFlag = 0;
        }
        String ossFilePath = ossUrl;
        try {
            if (imageData == null || "".equals(imageData)) {
                return "";
            }
            if (editFlag == 1) {
                String serverPath = request.getSession().getServletContext()
                        .getRealPath("/");
                String fileName = UUID.randomUUID().toString();
                String imgFilePath = serverPath + "/" + fileName + ".jpg";
                //将base64码转换成字节流
                ByteArrayInputStream inputStaream = downloadImgae(imageData, "");
//根据坐标画分数
                drawScore(drawjsonArray, ossUrl, inputStaream, imgFilePath);
                //上传到oss
                ossFilePath = OSSUtils.uploadFile(
                        XuexinConstants.OSS_WORD_BUCKET, imgFilePath, ossUrl); 
            } 
        } catch (Exception e) {
            log.error("图片上传异常" + ossUrl);
            e.printStackTrace();
        }
        return ossFilePath;
    }



 /**
     * 
     * 方法描述
     *
     * @param imageData
     * @param serverPath
     * @return
     * @throws IOException
     * @version v1.0
     * @author mayong 
     */
    public ByteArrayInputStream downloadImgae(String imageData,
            String serverPath) throws IOException {
        Base64 base64 = new Base64();
        byte[] k = base64.decode(imageData.substring("data:image/jpg;base64,"
                .length()));
        ByteArrayInputStream in = new ByteArrayInputStream(k);
        return in;
    }


/**
     * 画分数
     * 方法描述
     *
     * @param imageData
     * @param serverPath
     * @return
     * @throws IOException
     * @version v1.0
     * @author mayong
     * @throws JSONException  
     */
    public void drawScore(String drawjsonArray, String ossUrl,
            ByteArrayInputStream inputStaream, String imgFilePath)
        throws IOException, JSONException {
        URL url = new URL("http://" + ossUrl);
        //题块原图
        InputStream imagein = new BufferedInputStream(url.openStream());
        BufferedImage image = ImageIO.read(imagein);
        //线图
        //        InputStream imagein2 = new FileInputStream(imgFilePath);
        BufferedImage image2 = ImageIO.read(inputStaream);
        //        Graphics g = image.createGraphics();

//这里直接通过读取图片字节的方式会使,画出来的图颜色失真,所以转换了一下,没有找到原因,所以在此转换了一下
        BufferedImage images = new BufferedImage(image.getWidth(),
                image.getHeight(), BufferedImage.TYPE_INT_BGR);
//创建画笔
        Graphics g = images.createGraphics();
        //画题块图片
        g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
        //画判分线
        g.drawImage(image2, 0, 0, image.getWidth(), image.getHeight(), null);
        //画分值
        JSONArray jsonArray = new JSONArray(drawjsonArray);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject groupJson = jsonArray.getJSONObject(i);
            String step = groupJson.get("step") + "";
//获取分数坐标
            Integer y1 = Integer.parseInt(new java.text.DecimalFormat("0")
                    .format(groupJson.get("y1")));
            Integer x2 = Integer.parseInt(new java.text.DecimalFormat("0")
                    .format(groupJson.get("x2")));
            if (x2 > (image.getWidth() - 22) && image.getWidth() < 676) {
                x2 = image.getWidth() - 22;
            } else if (image.getWidth() > 675 && x2 > 675) {
                x2 = 676 - 22;
            }
//画分值图片
            //            String drawImge = "steps03.png";
            //            if (step.equals("0.5")) {
            //                drawImge = "step_half.png";
            //            } else if (step.equals("1")) {
            //                drawImge = "steps01.png";
            //            } else if (step.equals("2")) {
            //                drawImge = "steps02.png";
            //            } else if (step.equals("3")) {
            //                drawImge = "steps03.png";
            //            }
            //            ImageIcon imageIconQ1 = new ImageIcon(serverPath + "/image/"
            //                    + drawImge);
            Integer x2_flag = 0;
            Integer y1_flag = 0;
            //            Integer iconWidth = 0;
            //            Integer iconHeight = 0;
            int fontSize = 20;
//计算分数图片坐标
            if (image.getWidth() > 675) {
                x2_flag = (x2 - 22) * image.getWidth() / 676;
                y1_flag = (y1 - 3) * image.getWidth() / 676;
                //                iconWidth = imageIconQ1.getIconWidth() * image.getWidth() / 676;
                //                iconHeight = imageIconQ1.getIconHeight() * image.getWidth()
                //                        / 676;
                fontSize = 38;
            } else {
                x2_flag = x2 - 15;
                y1_flag = y1 - 3;
                //                iconWidth = imageIconQ1.getIconWidth();
                //                iconHeight = imageIconQ1.getIconHeight();
            }
//用画笔画分数图片
            //            g.drawImage(imageIconQ1.getImage(), x2_flag, y1_flag, iconWidth,
            //                    iconHeight, null);//
//设置分数字段样式
            Font font = new Font(null, Font.ITALIC, fontSize); // 38号字
            g.setFont(font);
            g.setColor(Color.RED);
//用画笔画分数
            g.drawString(step, x2_flag, y1_flag);


        }
        g.dispose();
        OutputStream outImage = new FileOutputStream(imgFilePath);
        JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(outImage);
        enc.encode(images);
        outImage.flush();
        imagein.close();
        inputStaream.close();
        outImage.close();
    }



  /**
     * 
     * 方法描述
     *
     * @param srcDir 本地源文件目录,包括文件名
     * @param dstDir OSS目标文件目录,包括文件名
     * @return
     * @throws Exception
     * @version v1.0 
     */
    public static String uploadFile(String bucketName, String srcFile,
            String dstFile) {


        // 判断源文件是否存在
        File f = new File(srcFile);
        if (!f.exists()) {
            LOG.debug(srcFile + ":文件未找到");
            return null;
        }
        BufferedInputStream is = null;
        try {
            // 读取文件
            is = new BufferedInputStream(new FileInputStream(f));
            ObjectMetadata thuObjectMeta = new ObjectMetadata();
            thuObjectMeta.setContentLength(f.length());
            // 上传到oss
            client.putObject(bucketName, dstFile, is, thuObjectMeta);
            return bucketName + "@" + dstFile;
        } catch (IOException e) {
            e.printStackTrace();
            LOG.debug(e.getMessage(), e);
        } finally {
            try {
                is.close();
                f.delete();// 删除无用的文件
            } catch (IOException e) {
                LOG.debug(e.getMessage(), e);
            }
        }
        return null;
    }

猜你喜欢

转载自blog.csdn.net/tianmaaa/article/details/78929077