android– 使用NDK从本机内存恢复图像返回没有显示的黑色图像

android– 使用NDK从本机内存恢复图像返回没有显示的黑色图像,第1张

概述我试图从本机内存恢复图像(使用NDK,C/C++),但这会返回一个黑色图像.我在做什么::>1)从Drawable获取图像>2)将旋转应用于图像>3)旋转后将灰度效果应用于图像>4)最后我试图将灰度图像保存在SD卡中对于上述所有步骤,我指的是thisawesomelib,它具有存储和恢复映像的本机方法.

我试图从本机内存恢复图像(使用NDK,C/C++),但这会返回一个黑色图像.

我在做什么 ::

> 1)从Drawable获取图像
> 2)将旋转应用于图像
> 3)旋转后将灰度效果应用于图像
> 4)最后我试图将灰度图像保存在SD卡中

对于上述所有步骤,我指的是this awesome lib,它具有存储和恢复映像的本机方法.

请注意图像存储在SD卡中但是当我试图看到图像时,它完全是黑色的,根本没有显示.

我的Java实现::

public boolean onoptionsItemSelected(MenuItem item) {        switch (item.getItemID())        {        case R.ID.item_rotate_90:            options.inPreferredConfig = Config.ARGB_8888;            bitmapOrig = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_cam,options);            storeBitmap(bitmapOrig);            bitmapOrig.recycle();            rotateBitmap(90,_handler);            tempBmp=getBitmapAndFree();            bitmapWip = Bitmap.createBitmap(bitmapOrig.getWIDth(),bitmapOrig.getHeight(),Config.Alpha_8);            jniConvertToGray(tempBmp,bitmapWip);            if(bitmapWip!=null)            {                try                 {                    Bitmap b = Bitmap.createBitmap(bitmapWip.getWIDth(),bitmapWip.getHeight(),Bitmap.Config.ARGB_8888);                    Canvas c = new Canvas(b);                    Paint paint = new Paint();                    colorMatrix cm = new colorMatrix();                    colorMatrixcolorFilter f = new colorMatrixcolorFilter(cm);                    paint.setcolorFilter(f);                    c.drawBitmap(bitmapWip, 0, 0, paint);                    storeBitmap(b);                    SaveGrayScaledImage(b);                    b.recycle();                    tempBmp.recycle();                } catch (IOException e) {                    e.printstacktrace();                }                ivdisplay.setimageBitmap(bitmapWip);            }            break;        }}

我没有对本机方法进行任何更改(意味着使用与this lib相同的方法来存储和恢复映像).

将图像保存到SD卡::

private voID SaveGrayScaledImage(Bitmap finalBitmap)throws IOException {        String imagefilename = "Temp" + "_gray";        file albumF = new file("/mnt/sdcard/","gray_img");        if(!albumF.exists())        {            albumF.mkdirs();        }        // file imageF = file.createTempfile(imagefilename, JPEG_file_SUFFIX,        // albumF);        file imageF = new file(albumF,imagefilename + ".jpeg");        if (imageF.exists()) {            imageF.delete();            imageF.createNewfile();        }        try {            fileOutputStream out = new fileOutputStream(imageF);            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);            out.flush();            out.close();        } catch (Exception e) {            e.printstacktrace();            imageF = null;        }}

谷歌搜索时,我发现(可能是我错了)返回本机内存的图像有Alpha_8位图配置,所以我转换配置Alpha_8 t0 ARGB_8888,但结果是一样的.

将位图从Alpha_8转换为ARGB_8888 ::

Bitmap b = Bitmap.createBitmap(bitmapWip.getWIDth(),bitmapWip.getHeight(),Bitmap.Config.ARGB_8888);Canvas c = new Canvas(b);Paint paint = new Paint();colorMatrix cm = new colorMatrix();colorMatrixcolorFilter f = new colorMatrixcolorFilter(cm);paint.setcolorFilter(f);c.drawBitmap(bitmapWip, 0, 0, paint);

StoreBimap功能::

public voID storeBitmap(final Bitmap bitmap){    if(_handler!=null)        freeBitmap();    _handler=jniStoreBitmapData(bitmap);}

我不知道我哪里错了.我一次又一次地检查了lib方法和implmentation以找到问题.

我花了很多时间在这个小问题上,这真让我感到沮丧.
如果您需要我身边的任何其他信息,请告诉我.
请帮我解决这个问题.

提前谢谢了….

编辑::

bitmapHolder=new JniBitmapHolder();    final Options options=new Options();    BitmapFactory.decodefile(picPath, options);    options.inJustDecodeBounds=true;             options.inPreferredConfig=Config.ARGB_8888;             prepareForDownsampling(options,192,256);             System.gc();             bmpGrayscale=BitmapFactory.decodefile(picPath,options);             int wIDth = bmpGrayscale.getWIDth();             int height = bmpGrayscale.getHeight();             bitmapHolder.storeBitmap(bmpGrayscale);             bmpGrayscale.recycle();   Bitmap thumbnail = null;   int rotationIndegrees = 0;   if (picPath != null) {    Uri uri = Uri.parse(picPath);    ExifInterface exif = null;    try {     exif = new ExifInterface(uri.getPath());    } catch (IOException e) {     // Todo auto-generated catch block     e.printstacktrace();    }    int rotation = exif.getAttributeInt(      ExifInterface.TAG_ORIENTATION,      ExifInterface.ORIENTATION_norMAL);    rotationIndegrees = exifTodegrees(rotation);   }   rotationIndegrees = 90;    ByteBuffer _handler =null;    switch(rotationIndegrees)               {               case 90:                 bitmapHolder.rotateBitmapCw90();                 break;               case 180:                 bitmapHolder.rotateBitmap180();                 break;               }    Bitmap bitmapWip = Bitmap.createBitmap(wIDth,height,Config.Alpha_8);    bitmapHolder.bitmapGrayScale(bitmapWip);     if(bitmapWip!=null){      file Currentfile = saveGrayScaledIamge(bitmapWip,        takePhotofile);     }

我已经按照你的建议/步骤但结果是一样的,得到没有显示的黑色图像.

解决方法:

好的我发现了多个问题和改进提示:

>第一个createBitmap在宽度*高度上运行,位图旋转而不是高度*宽度.这应该是:

rotateBitmap(90,_handler);tempBmp=getBitmapAndFree();bitmapWip=Bitmap.createBitmap(bitmapOrig.getHeight(),bitmapOrig.getWIDth(),Config.Alpha_8);

>保存文件时,您没有获得正确的路径(使用硬编码路径,lint会对其进行警告).
> jniConvertToGray实际上不需要遍历数组,只能使用指针,因为它只运行在一个像素上.您将位图存储到JNI两次而不是一次(只需执行:存储,旋转,灰度,恢复和免费).
>完成工作后不要使用新的位图,所以如果我多次调用旋转,它似乎什么都不做.
>你已经有bitmapWip旋转和灰度.为什么你需要制作一个包含其内容的新位图,对其进行灰度,然后保存?
>函数应在其名称的开头用小写字母命名.
>最后,最重要的是:您使用Alpha_8显示您需要保存到文件的图像.这种配置没有颜色.这是一个面具.要查看问题,您应该为imageVIEw设置背景颜色:

ivdisplay.setBackgroundcolor(0xFFff0000);

在选择旋转之前,你什么也看不到红色.选择之后,你认为白色的东西实际上变成了红色.那是因为它是透明的……

如果在开发的任何阶段,您已成功将图像保存到文件并认为它是黑色图像(但大小不是0),请尝试编辑它并在其后面添加背景.也许你很幸运,只有透明像素……

添加将文件保存为jpg格式(不支持透明度)这一事实也可能导致意外行为.

为了解决这个问题,你应该使用我用过的相同技术 – 一直使用一个位图.没有必要创造这么多. java世界中只应存在一个,它应该支持颜色.

总结

以上是内存溢出为你收集整理的android – 使用NDK从本机内存恢复图像返回没有显示的黑色图像全部内容,希望文章能够帮你解决android – 使用NDK从本机内存恢复图像返回没有显示的黑色图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1099784.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存