nds运行java_Java Raster.getNumBands方法代码示例

import java.awt.image.Raster; //导入方法依赖的package包/类

/** Encode an image to JPEG **/

public ByteBuffer encode(Raster img, int quality) throws TurboJpegException {

Pointer codec = null;

Pointer bufPtr = null;

try {

TJPF pixelFmt;

switch (img.getNumBands()) {

case 4:

pixelFmt = TJPF.TJPF_BGRX; // 4BYTE_BGRA

break;

case 3:

pixelFmt = TJPF.TJPF_BGR; // 3BYTE_BGR

break;

case 1:

pixelFmt = TJPF.TJPF_GRAY; // 1BYTE_GRAY

break;

default:

throw new IllegalArgumentException("Illegal sample format");

}

TJSAMP sampling = pixelFmt == TJPF.TJPF_GRAY ? TJSAMP.TJSAMP_GRAY : TJSAMP.TJSAMP_444;

codec = lib.tjInitCompress();

// Allocate JPEG target buffer

int bufSize = (int) lib.tjBufSize(img.getWidth(), img.getHeight(), sampling);

bufPtr = lib.tjAlloc(bufSize);

NativeLongByReference lenPtr = new NativeLongByReference(bufSize);

// Wrap source image data buffer with ByteBuffer to pass it over the ABI

ByteBuffer inBuf = ByteBuffer.wrap(((DataBufferByte) img.getDataBuffer()).getData())

.order(runtime.byteOrder());

int rv = lib.tjCompress2(

codec, inBuf, img.getWidth(), 0, img.getHeight(), pixelFmt,

new PointerByReference(bufPtr), lenPtr, sampling, quality, 0);

if (rv != 0) {

LOG.error("Could not compress image (dimensions: {}x{}, format: {}, sampling: {}, quality: {}",

img.getWidth(), img.getHeight(), pixelFmt, sampling, quality);

throw new TurboJpegException(lib.tjGetErrorStr());

}

ByteBuffer outBuf = ByteBuffer.allocate(lenPtr.getValue().intValue()).order(runtime.byteOrder());

bufPtr.get(0, outBuf.array(), 0, lenPtr.getValue().intValue());

outBuf.rewind();

return outBuf;

} finally {

if (codec != null && codec.address() != 0) lib.tjDestroy(codec);

if (bufPtr != null && bufPtr.address() != 0) lib.tjFree(bufPtr);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值