【问题标题】:Why i cannot show my byte[] image after converting it from bitmap?为什么从位图转换后无法显示我的 byte[] 图像?
【发布时间】:2015-05-21 19:14:06
【问题描述】:

我正在尝试编写一个接受图像(Bitmap) 并返回byte[] array 的方法。最后,我试着把这个byte[] array写到一个文件夹里,这样我就可以看到区别了,但是我的byte[] array不能显示,另外,它没有按比例缩小!这是我的方法:

private byte[] changeSize(Bitmap image) {
        byte[] picture;
        int width = image.getWidth();
        int height = image.getHeight();
        int newHeight = 0, newWidth = 0;
            if (width > 250 || height > 250) {
                    if (width > height) { //landscape-mode
                        newHeight = 200;
                        newWidth = (newHeight * width) / height;
                    } else { //portrait-mode
                        newWidth = 200;
                        newHeight = (newWidth * height) / width;
                    }
                } else {
                    Toast.makeText(this, "Something wrong!", Toast.LENGTH_LONG).show();
                }
        Bitmap sizeChanged = Bitmap.createScaledBitmap(image, newWidth, newHeight, true);
        //Convert bitmap to a byte array
        int bytes = sizeChanged.getByteCount(); 
        ByteBuffer bb = ByteBuffer.allocate(bytes); 
        sizeChanged.copyPixelsFromBuffer(bb); 
        picture = bb.array();
        //Write to a hd
        picturePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        String fileName = edFile.getText().toString() + "_downscaled" + ".jpg";
        File file = new File(picturePath, fileName);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            fos.write(picture);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;
} 

我尝试了几个小时让我的byte[] array 可见,但我根本做不到。非常感谢任何帮助或提示我出轨的地方。

【问题讨论】:

  • 您从copyPixelsFromBuffer() 获得的字节数组包含出现在主内存中的原始 图像像素数据。此数据肯定是不是 JPEG 编码的,而是需要根据位图的像素格式解释的值数组。你真正想做什么?
  • 1.相对于宽度/高度缩小我的位图。 2. 获取新位图sizeChanged。 3.将新的Bitmap实例转换为byte[] array´ --> byte[]图片. 4. writing picture`到文件系统中查看。

标签: java android bitmap bytearray scale


【解决方案1】:

这对我有用

public static Bitmap byteArraytoBitmap(byte[] bytes) {
    return (BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
}

public static byte[] bitmaptoByteArray(Bitmap bmp) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); //PNG format is lossless and will ignore the quality setting!
    byte[] byteArray = stream.toByteArray();
    return byteArray;
}

public static Bitmap bitmapFromFile(File file) {
    //returns null if could not decode
    return BitmapFactory.decodeFile(file.getPath());
}

public static boolean saveImage(Bitmap image, String filePath) {
    LogInfo(TAG, "Saving image to: " + filePath);
    File file = new File(filePath);
    File fileDirectory = new File(file.getParent());
    LogInfo(TAG, fileDirectory.getPath());

    if (!fileDirectory.exists()) {
        if (!fileDirectory.mkdirs()) {
            Log.e(TAG, "ERROR CREATING DIRECTORIES");
            return false;
        }
    }

    try {
        file.createNewFile();
        FileOutputStream fo = new FileOutputStream(file);
        fo.write(bitmaptoByteArray(image));
        fo.flush();
        fo.close();
        return true;
    }
    catch (Exception e) {
        e.printStackTrace();
        return false;
    }

}

【讨论】:

  • 我尝试了你的方法:bitmaptoByteArray,它很顺利,我的意思是转换(bitmapbyte [])。但我不明白为什么缩小后的大小(88Kb)> 之前的大小(83KB)?包括尺寸在内的目标医学缩小也是为了减小尺寸! - 结果来自我刚刚进行的测试。
  • 如果要缩小尺寸,请将格式更改为 jpeg 并降低质量。说原版的 70-80%。强硬我同意较小的图像确实应该占用更少的空间。出了点问题。
  • 现在一切顺利。感谢您抽出宝贵的时间来帮助我,我为此花费了很多时间。
猜你喜欢
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
  • 2012-03-10
  • 1970-01-01
  • 2021-04-21
  • 2016-01-29
  • 2019-09-29
相关资源
最近更新 更多