2011-08-19 26 views
7

我在拍攝照片後使用Android相機代碼byte [] imageData參數爲null,我不知道爲什麼。Android的圖片回調數據爲空

package com.pictures; 

import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 


import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.PixelFormat; 
import android.graphics.Bitmap.CompressFormat; 
import android.hardware.Camera; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.MotionEvent; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.view.View.OnClickListener; 
import android.view.View.OnTouchListener; 

public class CamaraView extends Activity implements SurfaceHolder.Callback, 
     OnClickListener { 
    static final int FOTO_MODE = 0; 
    private static final String TAG = "CameraTest"; 
    Camera mCamera; 
    boolean mPreviewRunning = false; 
    private Context mContext = this; 

    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 

     Log.e(TAG, "onCreate"); 

     Bundle extras = getIntent().getExtras(); 

     getWindow().setFormat(PixelFormat.TRANSLUCENT); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.main); 
     mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera); 
     mSurfaceView.setOnClickListener(this); 
     mSurfaceHolder = mSurfaceView.getHolder(); 
     mSurfaceHolder.addCallback(this); 
     mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    } 

    @Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) { 
     super.onRestoreInstanceState(savedInstanceState); 
    } 

    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { 
     public void onPictureTaken(byte[] imageData, Camera c) { 

      if (imageData != null) { 

       Intent mIntent = new Intent(); 

       StoreByteImage(mContext, imageData, 50, 
         "ImageName"); 
       mCamera.startPreview(); 

       setResult(FOTO_MODE, mIntent); 
       finish(); 

      } 
     } 
    }; 

    protected void onResume() { 
     Log.e(TAG, "onResume"); 
     super.onResume(); 
    } 

    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
    } 

    protected void onStop() { 
     Log.e(TAG, "onStop"); 
     super.onStop(); 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
     Log.e(TAG, "surfaceCreated"); 
     mCamera = Camera.open(); 

    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
     Log.e(TAG, "surfaceChanged"); 


     // XXX stopPreview() will crash if preview is not running 
     if (mPreviewRunning) { 
      mCamera.stopPreview(); 
     } 

     Camera.Parameters p = mCamera.getParameters(); 
     p.setPreviewSize(w, h); 
     mCamera.setParameters(p); 

     try { 
      mCamera.setPreviewDisplay(holder); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      System.out.println("Caught exception in surface chagned"); 
      e.printStackTrace(); 
     } 
     mCamera.startPreview(); 
     mPreviewRunning = true; 

    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     Log.e(TAG, "surfaceDestroyed"); 
     mCamera.stopPreview(); 
     mPreviewRunning = false; 
     mCamera.release(); 
    } 

    private SurfaceView mSurfaceView; 
    private SurfaceHolder mSurfaceHolder; 

    public void onClick(View arg0) { 

     mCamera.takePicture(null, mPictureCallback, mPictureCallback); 

    } 

    public static boolean StoreByteImage(Context mContext, byte[] imageData, 
      int quality, String expName) { 

     File sdImageMainDirectory = new File("/sdcard"); 
     FileOutputStream fileOutputStream = null; 
     String nameFile; 
     try { 

      BitmapFactory.Options options=new BitmapFactory.Options(); 
      options.inSampleSize = 5; 

      Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, 
        imageData.length,options); 


      fileOutputStream = new FileOutputStream(
        sdImageMainDirectory.toString() +"/image.jpg"); 


      BufferedOutputStream bos = new BufferedOutputStream(
        fileOutputStream); 

      myImage.compress(CompressFormat.JPEG, quality, bos); 

      bos.flush(); 
      bos.close(); 

     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return true; 
    } 

} 
+0

究竟是什麼?指向您在回叫中收到的數據,SD卡上文件的內容? –

+0

嗨,public void onPictureTaken(byte [] data,Camera camera)在這一行的數據參數爲null ... – max

+0

嗯,這可能有很多理由 - 相機配置在某種程度上是棘手的。有必要看看你如何配置相機,並請求回調 –

回答

9

好的,我看到了問題。您正在重新使用raw和jpeg回調的圖片回調。從javadoc您可以閱讀:

觸發異步圖像捕獲。隨着圖像捕獲 的進展,相機服務 嚮應用程序發起一系列回調。拍攝圖像後會發生快門回調。 這可以用來觸發聲音,讓用戶知道圖像 已被捕獲。原始回調在原始圖像數據爲 可用時發生(注意:如果沒有原始圖像,數據將爲空 回調緩衝區可用或原始圖像回調緩衝區不是足夠大以容納原始圖像)。 postview回調發生在 縮放,完全處理的postview圖像可用時(注意:並非所有的 硬件都支持此功能)。當壓縮的 圖像可用時,會發生jpeg回調。如果應用程序不需要特定的回調,則可以傳遞null而不是回調方法。

所以如果它被稱爲原始回調,數據可能爲空。 如果您不喜歡原始數據,請使用:

mCamera.takePicture(null, null, mPictureCallback); 
+0

@康斯坦丁普里布魯達感謝和很多工作。 – max

+1

它不適用於摩托車! – Toshe

+0

那麼,在我的情況下,一切正常工作在三星銀河音符和其他高端設備上,它甚至在仿真器上效果很好,但在像HTC Wildfire S和其他這樣的低端設備,一些 –