android  cemera  在预览时保存图片,
通过onPreviewFrame(byte[] aData, Camera arg1),将aData保存为jpg格式的图片,保存失败?以下是code
 public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
     Log.e(TAG,"surfaceCreated");
     saveRawDataThread = new SaveRawDataThread();
        camera = Camera.open();
        
        Camera.Parameters parameters = camera.getParameters();
        /* 设置拍照的图片格式 */
        parameters.getSupportedPreviewFormats();
        parameters.setPreviewFormat(ImageFormat.RGB_565);
        
        parameters.setPreviewFrameRate(5);
       
        camera.setParameters(parameters);
        camera.startPreview();
        
        try {
camera.setPreviewDisplay(holder); camera.setPreviewCallback(new PreviewCallback() { public void onPreviewFrame(byte[] aData, Camera arg1) {

FileOutputStream outStream = null;

//if(mAutoFocus)
{
try {
outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));
outStream.write(aData);
outStream.close(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
Preview.this.invalidate();
}
});
} catch (IOException e) {
e.printStackTrace();
}
    }

解决方案 »

  1.   

    不晓得LZ所用的硬件平台是怎么实现的,
    应该是preview中出来的数据应该是没有被编码成jpeg格式的吧,
    拍照出来的回调函数中传回的数据才是被编码成jpeg格式的吧。
    所以出来的应该是RGB的或者YUV格式的数据,
    直接加个jpg后缀存起来打开是肯定不行的。
    自己去找个转换或者查看工具再去看下写的数据
      

  2.   


    谢谢你
    但是parameters.setPreviewFormat(ImageFormat.RGB_565);
    这里已经设置为 RGB了
    还是不对
    我用 public void decodeYUV420SP(byte[] rgbBuf, byte[] yuv420sp, int width, int height) {
        final int frameSize = width * height;
            if (rgbBuf == null)
                throw new NullPointerException("buffer 'rgbBuf' is null");
            if (rgbBuf.length < frameSize * 3)
                throw new IllegalArgumentException("buffer 'rgbBuf' size "
                 + rgbBuf.length + " < minimum " + frameSize * 3);         if (yuv420sp == null)
                throw new NullPointerException("buffer 'yuv420sp' is null");          if (yuv420sp.length < frameSize * 3 / 2)
                throw new IllegalArgumentException("buffer 'yuv420sp' size " + yuv420sp.length
                  + " < minimum " + frameSize * 3 / 2);      int i = 0, y = 0;
         int uvp = 0, u = 0, v = 0;
         int y1192 = 0, r = 0, g = 0, b = 0;      for (int j = 0, yp = 0; j < height; j++) {
              uvp = frameSize + (j >> 1) * width;
              u = 0;
              v = 0;
             for (i = 0; i < width; i++, yp++) {
                  y = (0xff & ((int) yuv420sp[yp])) - 16;
                 if (y < 0) y = 0;
                 if ((i & 1) == 0) {
                      v = (0xff & yuv420sp[uvp++]) - 128;
                      u = (0xff & yuv420sp[uvp++]) - 128;
                  }               y1192 = 1192 * y;
                  r = (y1192 + 1634 * v);
                  g = (y1192 - 833 * v - 400 * u);
                  b = (y1192 + 2066 * u);              if (r < 0) r = 0; else if (r > 262143) r = 262143;
                 if (g < 0) g = 0; else if (g > 262143) g = 262143;
                 if (b < 0) b = 0; else if (b > 262143) b = 262143;               rgbBuf[yp * 3] = (byte)(r >> 10);
                  rgbBuf[yp * 3 + 1] = (byte)(g >> 10);
                  rgbBuf[yp * 3 + 2] = (byte)(b >> 10);
              }
          }
        }转换了  也不对请问您能详细点讲解一下吗?
    很感谢你呀
      

  3.   

    是这样的preview出来的数据格式是取决于硬件平台的实现的,
    就算是YUV的话,YUV也有好几种格式的,
    这个你需要去咨询下硬件平台的提供者,具体是camera的提供者,如果你能看得到所有源码的话也可以自己去源码里找。
    我没有具体研究过preview的数据是不是被编码成jpeg过,按照我的了解应该是没有被编过。
    所有你拿到的preview数据要存成jpeg格式的,你需要做的是把YUV或者RGB数据做JPEG编码操作。而不是像你调用这个函数,把YUV转成RGB565
      

  4.   

    我在做相机开发,想设置其焦距
    Camera.getParameters().isZoomSupported() -->true
    Camera.getParameters().isSmoothZoomSupported() -->false这种情况下,我怎么才能实现变焦呢??