2018年11月29日木曜日

File upload



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>File Upload</title>
</head>
<body>
    <form action="/sample/upload" method="post" enctype="multipart/form-data">

        <p>
        <select name="example1">
        <option value="サンプル1">選択肢のサンプル1</option>
        <option value="サンプル2">選択肢のサンプル2</option>
        <option value="サンプル3">選択肢のサンプル3</option>
        <option value="サンプル4">選択肢のサンプル4</option>
        <option value="サンプル5">選択肢のサンプル5</option>
        </select>
        </p>

        <input type="file" name="upload_file" /><br />
        <button type="submit">upload</button>
    </form>
</body>
</html>


package com.example.demo.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.nio.charset.Charset;
import java.util.List;

import javax.validation.Constraint;
import javax.validation.Payload;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.example.demo.domain.Fruit;
import com.example.demo.mapper.FruitMapper;

@Controller
public class IndexController {

    @Autowired
    FruitMapper fruitMapper;

    @RequestMapping(value="/i2/", method=RequestMethod.GET)
    public String index(Model model) {
        List<Fruit> list = fruitMapper.selectAll();
        model.addAttribute("fruits", list);
        return "index2";
    }

//    @RequestMapping(value = "/post", method = RequestMethod.POST)
//    public ModelAndView post(
//        HttpServletResponse response, @RequestParam("file") MultipartFile file){
//
//        try {
//            InputStream inputStream = null;
//            InputStreamReader inputStreamReader = null;
//            BufferedReader bufferReader = null;
//
//            inputStream = file.getInputStream();
//            inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
//            bufferReader = new BufferedReader(inputStreamReader);
//
//            String line;
//            while ((line = bufferReader.readLine()) != null) {
//
//                byte[] b = line.getBytes();
//                line = new String(b, "UTF-8");
//                String[] columns = line.split(",",-1);
//
//                for (int i = 0; i < columns.length; i++) {
//                    if(i == 0) {
//                        String colA = columns[i];
//                    } else if(i == 1) {
//                        String colB = columns[i];
//                    }
//                }
//            }
//        } catch (UnsupportedEncodingException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        return null;
//
//    }




//    @PostMapping("upload")
    @RequestMapping(value="/sample/upload", method=RequestMethod.POST)
    public String upload(
            @RequestParam("example1") String exampleStr,
            @RequestParam("upload_file") MultipartFile multipartFile,
            Model model
            ) throws IOException {
        model.addAttribute("originalFilename", multipartFile.getOriginalFilename());

        System.out.println("----------");

        InputStreamReader isr = new InputStreamReader(multipartFile.getInputStream(), Charset.forName("UTF-8"));
        BufferedReader br = new BufferedReader(isr);
        String line;
        while((line = br.readLine()) != null){
            System.out.println(line);;
        }

        System.out.println("----------");

        byte[] bytes = multipartFile.getBytes();
        System.out.write(bytes);

        System.out.println("----------");

        return "sample/result";
    }

    //    @PostMapping("upload")
//    @RequestMapping(value="/sample/upload", method=RequestMethod.POST)
//    public String upload(UploadForm uploadForm, Model model) {
//        model.addAttribute("originalFilename", uploadForm.getMultipartFile()
//                .getOriginalFilename());
//
//        return "sample/result";
//    }

    @Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Constraint(validatedBy = FileRequiredValidator.class)
    public @interface FileRequired {
        String message() default "{com.example.demo.validation.FileRequired.message}";

        Class<?>[] groups() default {};

        Class<? extends Payload>[] payload() default {};

        @Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
        @Retention(RetentionPolicy.RUNTIME)
        @Documented
        @interface List {
            FileRequired[] value();
        }
    }
   
}


2018年10月28日日曜日

■HelloWorld.java

package test;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import javax.imageio.ImageIO;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.imgcodecs.Imgcodecs;

public class HelloWorld {

    public static void fillColor(Mat mat, int r, int g, int b) {
        for (int row = 0; row < mat.size().height; ++row) {
            for (int col = 0; col < mat.size().width; ++col) {
                mat.put(row, col, g, b, r);
            }
        }
        return;
    }

    public static void pasteMat(Mat baseMat, Mat pasteMat, int pasteRow, int pasteCol) {
        for (int row = 0; row < pasteMat.size().height; ++row) {
            for (int col = 0; col < pasteMat.size().width; ++col) {
                baseMat.put(row, col, pasteMat.get(row, col));
            }
        }
        return;
    }

    public static void pasteMatTransparency(Mat baseMat, Mat pasteMat, int pasteRow, int pasteCol, int trR, int trG, int trB) {
        for (int row = 0; row < pasteMat.size().height; ++row) {
            for (int col = 0; col < pasteMat.size().width; ++col) {
                double[] bgr = pasteMat.get(row, col);
                if ((bgr[0] != trB) && (bgr[1] != trG) && (bgr[2] != trR)) {
                    baseMat.put(pasteRow + row, pasteCol + col, pasteMat.get(row, col));
                }
            }
        }
        return;
    }

    public static Image toBufferedImage(Mat m){
        int type = BufferedImage.TYPE_BYTE_GRAY;
        if ( m.channels() > 1 ) {
            type = BufferedImage.TYPE_3BYTE_BGR;
        }
        int bufferSize = m.channels()*m.cols()*m.rows();
        byte [] b = new byte[bufferSize];
        m.get(0,0,b); // get all the pixels
        BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
        final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
        System.arraycopy(b, 0, targetPixels, 0, b.length); 
        return image;
    }

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        ArrayList<ImageInfo> inputImageInfoList = new ArrayList<ImageInfo>();
        inputImageInfoList.add(new ImageInfo(1024, 655, "sw/baijin_rw.png", 110, 120));
        inputImageInfoList.add(new ImageInfo(1024, 655, "sw/baijin_rw.png", 210, 220));
        inputImageInfoList.add(new ImageInfo(1024, 710, "sw/tokyo_sw.png", 210, 220));
        inputImageInfoList.add(new ImageInfo(1024, 710, "sw/tokyo_sw.png", 310, 320));
        inputImageInfoList.add(new ImageInfo(1024, 1032, "sw/paris_rw.png", 410, 420));
        inputImageInfoList.add(new ImageInfo(1024, 1032, "sw/paris_rw.png", 510, 520));

        String markImagePath = "sw/mark.png";
        int markHotSpotX = 32;
        int markHotSpotY = 63;

        String outputImagePath = "result.png";

        int frameR = 201;
        int frameG = 202;
        int frameB = 203;

        int trR = 255;
        int trG = 255;
        int trB = 255;


        Mat markMat = Imgcodecs.imread(markImagePath);
        int frameCol = (int) markMat.size().width;
        int frameRow = (int) markMat.size().height;

        int outputImageHeight = 0;
        String lastFilePath = "";
        for (ImageInfo inputImageInfo : inputImageInfoList) {
            if (! inputImageInfo.filePath.equals(lastFilePath)) {
                lastFilePath = inputImageInfo.getFilePath();
                outputImageHeight += frameRow + inputImageInfo.getImageY();
            }
        }
        outputImageHeight += frameRow;;
        int outputImageWidth = frameCol * 2 + inputImageInfoList.get(0).getImageX();

        Mat outputMat = new Mat(outputImageHeight, outputImageWidth, CvType.CV_8UC3);

        fillColor(outputMat, frameR, frameG, frameB);


        lastFilePath = "";
        Mat inputMat = new Mat();
        int currRow = 0;
        int nextRow = 0;
        Mat roi = null;
        for (ImageInfo inputImageInfo : inputImageInfoList) {
            if (! inputImageInfo.filePath.equals(lastFilePath)) {
                lastFilePath = inputImageInfo.getFilePath();
                inputMat = Imgcodecs.imread(lastFilePath);
                currRow = nextRow;
                nextRow = (int) (currRow + frameRow + inputMat.size().height);
                roi = new Mat(outputMat, new Rect(frameCol, currRow + frameRow, inputImageInfo.getImageX(), inputImageInfo.getImageY()));
                pasteMat(roi, inputMat, currRow, frameCol);
            }
            pasteMatTransparency(roi, markMat, inputImageInfo.getMarkY() - markHotSpotY, inputImageInfo.getMarkX() - markHotSpotX, trR, trG, trB);
        }


        BufferedImage outputBufferedImage = new ImageTranslater().Mat2BI(outputMat);
        try (OutputStream outputStream = new FileOutputStream(outputImagePath)) {
            // 画像を出力する
            ImageIO.write(outputBufferedImage, "png", outputStream);
          } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        System.out.println("Done!");
    }
}

■ImageInfo.java

package test;

public class ImageInfo {
    int imageX;
    int imageY;
    String filePath;
    int markX;
    int markY;
    public ImageInfo(int imageX, int imageY, String filePath, int markX, int markY) {
        super();
        this.imageX = imageX;
        this.imageY = imageY;
        this.filePath = filePath;
        this.markX = markX;
        this.markY = markY;
    }
    public int getImageX() {
        return imageX;
    }
    public void setImageX(int imageX) {
        this.imageX = imageX;
    }
    public int getImageY() {
        return imageY;
    }
    public void setImageY(int imageY) {
        this.imageY = imageY;
    }
    public String getFilePath() {
        return filePath;
    }
    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
    public int getMarkX() {
        return markX;
    }
    public void setMarkX(int markX) {
        this.markX = markX;
    }
    public int getMarkY() {
        return markY;
    }
    public void setMarkY(int markY) {
        this.markY = markY;
    }
}

■ImageTranslater.java

package test;

import java.awt.image.BufferedImage;
import org.opencv.core.Mat;

/**
 * javaとprocessingとopencvのための画像オブジェクト変換用クラス
 * ビルド・パスにopencv-xxx.jar(openCV)とcore.jar(Processing)を追加しておいてください.
 *
 * @version 1.1
 */
public class ImageTranslater {

    /**
     * Mat型をBufferedImage型(TYPE_BYTE_GRAYまたはTYPE_3BYTE_BGR)に変換します
     * @param mat 変換したいMat型
     * @return 変換したBufferedImage
     */
    public static BufferedImage MatToBufferedImageBGR(Mat mat) {
        int dataSize = mat.cols() * mat.rows() * (int) mat.elemSize();
        byte data[] = new byte[dataSize];
        mat.get(0, 0, data);
        int type;
        if (mat.channels() == 1) {
            type = BufferedImage.TYPE_BYTE_GRAY;
        } else {
            type = BufferedImage.TYPE_3BYTE_BGR;
            for (int i = 0; i < dataSize; i += 3) {
                byte blue = data[i + 0];
                data[i + 0] = data[i + 2];
                data[i + 2] = blue;
            }
        }

        BufferedImage img = new BufferedImage(mat.cols(), mat.rows(), type);
        img.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), data);
        return img;
    }

    public BufferedImage Mat2BI(Mat mat) {
        return MatToBufferedImageBGR(mat);
    }

}