java向C++传图片

12 篇文章 0 订阅
案例一:http://www.java-gaming.org/index.php/topic,24988.0Thanks for everybody


Java code: change Java Image to int[] for JNI warpper

Image image = null; 
try {
File file = new File("c:/image.jpg");
image = ImageIO.read(file);
}
catch (MalformedURLException ex) {
Logger.getLogger(SmartTest.class.getName()).log(Level.SEVERE, ex.getMessage());
} catch (IOException ex) {
Logger.getLogger(SmartTest.class.getName()).log(Level.SEVERE, ex.getMessage());
}

// Image grabbing
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(image, x, y, w, h, pixels, 0, w);
try {
pg.grabPixels();
}catch (InterruptedException e) {
System.err.println("interrupted waiting for pixels!");
return;
}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
System.err.println("image fetch aborted or errored");
return;
}


JNI wrapper code(C++): change int[] of Java Image to C++ BITMAP


HBITMAP CreateBitmapFromPixel( HDC hDC, UINT uWidth, UINT uHeight, UINT uBitsPerPixel, LPVOID pBits )
{ 
HBITMAP m_HBitmap = NULL;
if ( !uWidth || !uHeight || !uBitsPerPixel )
{
return m_HBitmap;//hBitmap;
}
LONG lBmpSize = uWidth * uHeight * uBitsPerPixel / 8; 
BITMAPINFO bmpInfo = { 0 };
bmpInfo.bmiHeader.biBitCount = uBitsPerPixel;
bmpInfo.bmiHeader.biHeight = uHeight;
bmpInfo.bmiHeader.biWidth = uWidth;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biSizeImage = uWidth * uHeight * uBitsPerPixel / 8;
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
// Pointer to access the pixels of bitmap 
UINT * pPixels = 0;
/*hBitmap*/m_HBitmap = CreateDIBSection( hDC, (BITMAPINFO *)&bmpInfo, DIB_RGB_COLORS, (void **)& pPixels , NULL, 0); 
if ( !m_HBitmap )
return m_HBitmap;//hBitmap; // return if invalid bitmaps
SetBitmapBits( m_HBitmap, lBmpSize,pBits);
return m_HBitmap;
}


sample code to change Java Image to BITMAP in JNIwrapper :

int len = env->GetArrayLength(pixels); //pixels is jintArray and get from input parameter
jint* jarr;
jarr = env->GetIntArrayElements(pixels,FALSE);
HBITMAP hImg = CreateBitmapFromPixel(pFront->bmp.hdcColor,(DWORD)cx,(DWORD)cy,32,jarr);
if( hImg != NULL )
{
DIBSECTION dib;
BITMAPINFO bi;
::GetObject( hImg, sizeof(dib), &dib );
bi.bmiHeader = dib.dsBmih;

// HDC is Hardware Device context. For understand how to set it please read Microsoft MSDN
// w and h in here are belongs to BITMAP buffer based on you HDC
::SetDIBitsToDevice(HDC, 0, 0,w,h,0, 0, 0,dib.dsBm.bmHeight,dib.dsBm.bmBits,&bi,DIB_RGB_COLORS);

DeleteObject( hImg );
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值