将RGB图像转换为2.2中的位图错误

时间:2013-01-09 10:32:49

标签: android

将RGB图像转换为位图

public static Bitmap rgbToBitmap(int[] rgb, int width, int height) {
    if (rgb == null) throw new NullPointerException();

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    bitmap.setPixels(rgb, 0, width, 0, 0, width, height);
    return bitmap;
}

2 个答案:

答案 0 :(得分:1)

尝试这种方式:

 Bitmap bmp = Bitmap.createBitmap(frameWidth, frameHeight, Bitmap.Config.ARGB_8888); 

<强>编辑:

您可以使用以下方法解决out of memory问题:

 public static Bitmap shrinkBitmap(String p_file, int p_width, int p_height)
{
    BitmapFactory.Options m_bmpFactoryOptions = new BitmapFactory.Options();
    m_bmpFactoryOptions.inJustDecodeBounds = true;
    Bitmap m_bitmap = BitmapFactory.decodeFile(p_file, m_bmpFactoryOptions);        
    int m_heightRatio =
            (int) Math.ceil(m_bmpFactoryOptions.outHeight / (float) p_height);
    int m_widthRatio =
            (int) Math.ceil(m_bmpFactoryOptions.outWidth / (float) p_width);        
    if (m_heightRatio > 1 || m_widthRatio > 1)
    {
        if (m_heightRatio > m_widthRatio)
        {
            m_bmpFactoryOptions.inSampleSize = m_heightRatio;
        }
        else
        {
            m_bmpFactoryOptions.inSampleSize = m_widthRatio;
        }
    }       
    m_bmpFactoryOptions.inJustDecodeBounds = false;
    m_bitmap = BitmapFactory.decodeFile(p_file, m_bmpFactoryOptions);
    return m_bitmap;
}

希望它会对你有所帮助。

答案 1 :(得分:0)

尝试使用Bitmap.createScaledBitmap()与设备宽度和高度图像重载内存预算