【问题标题】:Convert raw Android image to png将原始Android图像转换为png
【发布时间】:2014-06-10 14:39:39
【问题描述】:

在有根的 Android 设备上,我想截屏并将原始格式的图像转换为 Png 图像,然后将其保存在本地。到目前为止,我设法访问了帧缓冲区,截取了屏幕截图并保存了原始图像。问题是当我将其转换为 Png 格式时,我得到的图像都是错误的......一堆白色和灰色的线条。 这是我所做的:

public void putRawImageInArray (byte [] array, File f ) throws IOException{
    @SuppressWarnings("resource")

    BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(f));   //The framebuffer raw image is in the file
    bufferedInputStream.read(array, 0, array.length);//read the file
} 

public void convertToBitmap (byte [] rawarray) throws IOException{
            byte [] Bits = new byte[rawarray.length*4]; 
            int i;
            for(i=0;i<rawarray.length;i++)
            {
                Bits[i*4] =
                    Bits[i*4+1] =
                    Bits[i*4+2] = (byte) ~rawarray[i]; 
                Bits[i*4+3] = -1;//0xff, that's the alpha.
            }


            Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            bm.copyPixelsFromBuffer(ByteBuffer.wrap(Bits)); 

            File f = new File(Environment.getExternalStorageDirectory(), "/pictures/picture.png");
            f.createNewFile();
            if (f.exists() == true) {
                f.delete();
            }
try{
            OutputStream fos=new FileOutputStream(f);
            bm.compress(CompressFormat.PNG, 100, fos);
            fos.close();
} catch (FileNotFoundException e) {
    Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
    Log.d(TAG, "Error accessing file: " + e.getMessage());
} 

我做错了什么?

【问题讨论】:

  • 您的图像显示为灰色,因为您将所有 RGB 值设置为相同(“灰色”的配方)。原始数据的颜色模型是什么?您假设它是 1 字节(颜色索引?)。将原始数据立即写入文件时,您是否获得可识别的数据?使用好的十六进制查看器进行检查。
  • 你能把代码sn-p粘贴到你打电话给convertToBitmap的地方吗?
  • 我从同一个活动中的一个类中调用它: class PullScreenAsyncTask extends AsyncTask { @Override public Void doInBackground(Void... params) { while(true) { / * #1:访问framebuffer,获取截图,将image.raw保存在文件中 #2:创建字节数组并调用putRawImageInArray() #3:调用convertToBitmap() */ }}}

标签: android png screenshot


【解决方案1】:

尝试删除所有这些

    byte [] Bits = new byte[rawarray.length*4]; 
    int i;
    for(i=0;i<rawarray.length;i++)
    {
        Bits[i*4] =
            Bits[i*4+1] =
            Bits[i*4+2] = (byte) ~rawarray[i]; 
        Bits[i*4+3] = -1;//0xff, that's the alpha.
    }

直接使用rawarray

 Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
 bm.copyPixelsFromBuffer(ByteBuffer.wrap(rawarray));

并确保您使用的颜色模型(Bitmap.Config.ARGB_8888)与图像数据的颜色模型相同。

【讨论】:

  • 感谢您的回答.. 当我删除该代码块时,我得到一个全黑的 png 图像。
猜你喜欢
  • 1970-01-01
  • 2014-01-30
  • 2019-11-08
  • 2014-10-01
  • 2022-01-09
  • 1970-01-01
  • 2022-10-02
  • 2014-06-14
  • 2011-02-01
相关资源
最近更新 更多