android - 使用 NDK 从 native 内存恢复图像返回无显示的黑色图像

标签 android c++ image bitmap android-ndk

我正在尝试从 native 内存中恢复图像(使用 NDK、C/C++),但返回的是黑色图像。

我在做什么::

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

    对于上述所有步骤,我指的是 this awesome lib,它有本地方法来存储和恢复图像。

请注 Intent 像正在存储在 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;
        }
}

我没有对 native 方法进行任何更改(意味着使用与 this 库相同的方法来存储和恢复图像)。

保存图片到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;
        }
}

在谷歌搜索时,我发现(可能是我错了)为 native 内存返回的图像具有 ALPHA_8 位图配置,因此我将配置 ALPHA_8 转换为 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 方法和实现以找到问题。

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

提前致谢....

编辑::

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 是在旋转的位图上使用 width*height 而不是 height*width 运行的。这应该是:

    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 从 native 内存恢复图像返回无显示的黑色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22953988/

相关文章:

css - 在 gmail 电子邮件消息中将图像显示在另一个图像之上

CSS图像不重复

javascript - 如何在android应用程序中访问localStorage中的数据

android - android中按钮的下拉菜单

android - RecyclerView 保存状态

Android Marshmallow,api 23,Cordova 应用程序上的权限被破坏

c++ - 有双向管道之类的东西吗?我希望输入和输出在两个简单程序之间交互

c++ - Unresolved external 问题,包括 aux_klib 内核库

c++ - std::vector 连续意义

javascript - 保护图像下载理论