1

PNG画像のいくつかの特性に基づいてPNG画像を調整するのに役立つJavaコードを実装しようとしています:例えば、色許容解釈タイプビット深度

0 1,2,4,8,16 各ピクセルはグレースケール サンプルです。

そこから、色の種類が 0 の場合、グレースケールの 1、2、4、8、16 の異なるビット深度に基づいてコードを実装する必要があることを検索しました。

Graphic2D ライブラリを使用したいので、次のように考えます。

 if (img_bitDepth == 16) {
            type = BufferedImage.TYPE_USHORT_GRAY; // 11
          } else if (img_bitDepth == 8) {
            type = BufferedImage.TYPE_BYTE_GRAY; //10
          } else if (img_bitDepth == 4) {
            type = BufferedImage.TYPE_BYTE_BINARY;
          } else if (img_bitDepth == 2) {
            type = BufferedImage.TYPE_BYTE_BINARY;
          } else if (img_bitDepth == 1) {
            type = BufferedImage.TYPE_BYTE_BINARY;
          } else {
            //logger warning.
          }

     BufferedImage resizedImage = new BufferedImage (img_width, img_height, type);

      Graphics2D g = resizedImage.createGraphics();
      g.drawImage(originalImage, 0, 0, img_width, img_height, null);
      g.dispose();

しかし、画像タイプ「TYPE_BYTE_BINARY」で2と4のビット深度を設定する方法がわかりません。

なにか提案を?

4

1 に答える 1

0

私はこのように使用しようとしていますが、うまくいくようです。

 private static final IndexColorModel createGreyscaleModel(int bitDepth) {

    // Only support these bitDepth(1, 2, 4) for now: Set the size.
    int size = 0;
    if (bitDepth == 1 || bitDepth == 2 || bitDepth == 4) {
      size = (int) Math.pow(2, bitDepth);
    } else {
      //logger error
      return null;
    }

    // generate the rgb and set the greyscale color.
    byte[] r = new byte[size];
    byte[] g = new byte[size];
    byte[] b = new byte[size];

    // The size should be larger or equal to 2, so we firstly set the start and end pixel color.
    r[0] = g[0] = b[0] = 0;
    r[size-1] = g[size-1] = b[size-1] = (byte)255;

    for (int i=1; i<size-1; i++) {
       r[i] = g[i] = b[i] = (byte)((255/(size-1))*i);
    }
    return new IndexColorModel(bitDepth, size, r, g, b);
 }


 type = BufferedImage.TYPE_BYTE_BINARY;
            IndexColorModel cm = createGreyscaleModel(img_bitDepth);
            resizedImage = new BufferedImage (img_width, img_height, type, cm);

ありがとう。

于 2015-08-04T22:46:26.053 に答える