2013-02-05 98 views
0

当我用我的Android相机拍照时,我的屏幕上有一张图像(imageview3),我想用我的照片保存。Android:用照片上的图像保存照片

这里是我的onPictureTaken方法

public void onPictureTaken(byte[] data, Camera camera) { 
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/Ker"); 
    imagesFolder.mkdirs(); 
    String fileName = "Ker_.jpg"; 
    output = new File(imagesFolder, fileName); 
    ImageView view = (ImageView) gameactivity.findViewById(R.id.imageView3); 
    view.setDrawingCacheEnabled(true); 
    Bitmap b = view.getDrawingCache(); 
    FileOutputStream fos = null; 
    try { 
     fos = new FileOutputStream(output); 
    } catch (FileNotFoundException e1) { 
     e1.printStackTrace(); 
    } 
    b.compress(CompressFormat.JPEG, 95, fos); 
    try { 
     fos.write(data); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
     catch (IOException e) { 
     e.printStackTrace(); 
    } 
    camera.stopPreview(); 
} 

当我打开文件夹,保存的图片只是imageview3黑色背景。为什么真正的摄像头视图没有保存?

编辑 我试图用帆布太多的东西:

output = new File(imagesFolder, fileName); 
      ImageView view = (ImageView) gameactivity.findViewById(R.id.imageView3); 
      view.setDrawingCacheEnabled(true); 
      Bitmap b = view.getDrawingCache(); 
      FileOutputStream fos = null; 
      try { 
       fos = new FileOutputStream(output); 
       fos.write(data); 
       fos.close(); 
      } catch (FileNotFoundException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
       catch (IOException e) { 
        e.printStackTrace(); 
      } 
      FileOutputStream fos2 = null; 
      b.compress(CompressFormat.JPEG, 95, fos2); 

      try { 
       Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fos.getFD()); 
       Bitmap bitmap2 = BitmapFactory.decodeFileDescriptor(fos2.getFD()); 
       Canvas canvas = new Canvas(bitmap); 
       canvas.drawBitmap(bitmap2, null, null); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

是正确的吗?如何将画布保存到我的SD卡上的文件(即从文件输出流中的画布上写入数据)

回答

0

您正在将来自相机的imageview和JPEG的JPEG附加到单个文件中。

为每个文件创建一个单独的文件输出流,并将来自Bitmap.compress()和onPictureTaken数据数组的数据写入它们自己的流中。

如果您想要将两个图像合并为一个图像,则需要将数据数组解码为位图,然后使用Canvas将图像位图和相机捕获的位图绘制到画布上在你想要的安排中,然后把它保存为一个单独的jpeg。

您不能简单地连接两个压缩的JPEG比特流;该文件格式不起作用。

+0

你知道如何将Canvas保存到我的SD卡中的JPEG中,即如何从文件输出流的画布写入数据? – morg

+0

好了,谢谢http://www.jondev.net/articles/Combining_2_Images_in_Android_using_Canvas 重要的是我们必须保存“cs”而不是画布(请参阅链接) – morg