Category Archives: 잡동사니

opencv 얼굴인식 샘플

package com.jpbeta.tools.demo;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.videoio.VideoCapture;

public class Panel extends JPanel {
  private static final long serialVersionUID = 1L;
  private BufferedImage image;

  // Create a constructor method
  public Panel() {
    super();
  }

  private BufferedImage getimage() {
    return image;
  }

  private void setimage(BufferedImage newimage) {
    image = newimage;
    return;
  }

  /**
   * Converts/writes a Mat into a BufferedImage.
   *
   * @param matrix
   *          Mat of type CV_8UC3 or CV_8UC1
   * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY
   */
  public static BufferedImage matToBufferedImage(Mat matrix) {
    // "C:\\IDE\\pleiades_4.4\\workspace\\demo\\src\\main\\resources\\haarcascade_eye_tree_eyeglasses.xml"
    CascadeClassifier faceDetector = new CascadeClassifier("C:\\IDE\\pleiades_4.4\\workspace\\demo\\src\\main\\resources\\haarcascade_frontalface_alt.xml");
    MatOfRect faceDetections = new MatOfRect();

    faceDetector.detectMultiScale(matrix, faceDetections);
    for (Rect rect : faceDetections.toArray()) {
      Imgproc.rectangle(matrix, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0), 5);
    }
    // Imgcodecs.imwrite("fileName", matrix);

    int cols = matrix.cols();
    int rows = matrix.rows();
    int elemSize = (int) matrix.elemSize();
    byte[] data = new byte[cols * rows * elemSize];
    int type;
    matrix.get(0, 0, data);
    switch (matrix.channels()) {
    case 1:
      type = BufferedImage.TYPE_BYTE_GRAY;
      break;
    case 3:
      type = BufferedImage.TYPE_3BYTE_BGR;
      // bgr to rgb
      byte b;
      for (int i = 0; i < data.length; i = i + 3) {
        b = data[i];
        data[i] = data[i + 2];
        data[i + 2] = b;
      }
      break;
    default:
      return null;
    }
    BufferedImage image2 = new BufferedImage(cols, rows, type);
    image2.getRaster()
      .setDataElements(0, 0, cols, rows, data);
    return image2;
  }

  public void paintComponent(Graphics g) {
    BufferedImage temp = getimage();
    if (temp != null) {
      g.drawImage(temp, 10, 10, temp.getWidth(), temp.getHeight(), this);
    }
  }

  public static void main(String arg[]) {
    // Load the native library.
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    JFrame frame = new JFrame("BasicPanel");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
    Panel panel = new Panel();
    frame.setContentPane(panel);
    frame.setVisible(true);
    Mat webcam_image = new Mat();
    BufferedImage temp;
    VideoCapture capture = new VideoCapture();
    capture.open(0); // Useless

    if (capture.isOpened()) {
      while (true) {
        capture.read(webcam_image);
        if (!webcam_image.empty()) {
          Imgproc.resize(webcam_image, webcam_image, new Size(webcam_image.size().width * 0.3, webcam_image.size().height * 0.3));
          frame.setSize(webcam_image.width() + 40, webcam_image.height() + 60);
          temp = matToBufferedImage(webcam_image);
          panel.setimage(temp);
          panel.repaint();
        } else {
          System.out.println(" --(!) No captured frame -- ");
        }
      }
    }
    return;
  }
}

Java에서 임시 패스워드 만들기.

package xxx.xxx.util;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class PasswordUtil {

  public static final int KEY_LENTH = 2;

  /**
   * 初期パスワードを生成するメソッドです
   * 
   * @throws NoSuchAlgorithmException
   * @returnパスワード
   */
  public static String generateKey() throws NoSuchAlgorithmException {
    String resultStr = null;
    // 候補文字列
    String[] candidates = new String[] { "abcdefghijklmnopqrstuvwxyz",
        "0123456789" };
    int count = KEY_LENTH;
    char[] generated = new char[count];
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    StringBuffer candidateAll = new StringBuffer();
    int watermark = 0;
    for (int i = 0; i < candidates.length; i++) {
      if (candidates[i] != null && !"".equals(candidates[i])) {
        generated[watermark++] = candidates[i].charAt(sr.nextInt(candidates[i].length()));
        candidateAll.append(candidates[i]);
      }
    }
    for (int i = watermark; i < count; i++) {
      generated[i] = candidateAll.toString()
        .charAt(sr.nextInt(candidateAll.length()));
    }
    for (int i = 100000; i >= 0; i--) {
      int x = sr.nextInt(count);
      int y = sr.nextInt(count);
      char tmp = generated[x];
      generated[x] = generated[y];
      generated[y] = tmp;
    }
    resultStr = new String(generated);
    return resultStr;
  }// GEN-LAST:event_jButton1ActionPerformed

}

Apache Tomcat 에서 gzip 압축 설정..

최근 개발한 프로젝트가 Tomcat기반이여서

ie6으로 테스트한 결과 Javascript 에러가 계속 발생해서,

확인한 결과 클라이언트와 서버간의 통신문제 였습니다.

이것 땜에 몇일 버그잡기 했답니다.

ㅜㅜ

잊지 않기 위해 메모 해둠니다.

Apache Tomcat Configuration Reference – The HTTP Connector

http://tomcat.apache.org/tomcat-6.0-doc/config/http.html

compressionThe Connector may use HTTP/1.1 GZIP compression in an attempt to save server bandwidth. The acceptable values for the parameter is “off” (disable compression), “on” (allow compression, which causes text data to be compressed), “force” (forces compression in all cases), or a numerical integer value (which is equivalent to “on”, but specifies the minimum amount of data before the output is compressed). If the content-length is not known and compression is set to “on” or more aggressive, the output will also be compressed. If not specified, this attribute is set to “off”.

$CATALINA_HOME/conf/server.xml설정파일을 아래처럼 수정

<Server port=”8005″ shutdown=”SHUTDOWN”>
<Connector port=”8080″ protocol=”HTTP/1.1″
connectionTimeout=”20000″
redirectPort=”8443″
useBodyEncodingForURI=”true”
compression=”on”/>
</Server>

잊지 말자….