2013-10-08 46 views
0

我有以下代碼塊可以從前置攝像頭捕獲圖片並將其存儲到存儲器中。下面的代碼對於我嘗試過的所有設備(Samsung Galaxy S2,Note 2,Micromax Canvas 2,HD,Sony Xperia U)都非常適用,但是對於jpegcallback返回的「數據」在HTC One X上始終爲空。 ?Android:相機:jpeg回撥圖片數據null

public void takePicture() { 
     Log.d("camera","taking picture"); 
     // do we have a camera? 
     if (cameraIsOk()) { 
      Log.d("camera","waiting 1"); 

      try { 
       synchronized (this) { 
        wait(2000); 
       } 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       Log.d("camera", "Waiting didnt work!!"); 
       e.printStackTrace(); 
      } 
      if (myCamera == null) 
       myCamera = Camera.open(camId); 

      if (myCamera != null) 
      { 
       Log.d("camera","surface view"); 
       SurfaceView surfaceView = new SurfaceView(context); 
       surfaceView.setFocusable(true); 
       Log.d("camera","getting holder"); 
       SurfaceHolder holder = surfaceView.getHolder(); 
       Log.d("camera","add callback"); 
       holder.addCallback(this); 
       Log.d("camera","set type"); 
       holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

       try { 
        Log.d("camera","get parameters"); 
        Camera.Parameters parameters = myCamera.getParameters(); 
        Log.d("camera","get supported preview size"); 
        List<Size> sizes = parameters.getSupportedPreviewSizes(); 
        Size optimalSize = GBCameraUtil.getOptimalPreviewSize(sizes, 1024, 768); 

        int sdkBuildVersion = Integer.parseInt(android.os.Build.VERSION.SDK); 

        if (sdkBuildVersion < 5 || usingLandscape) 
        { 
         // Picture size should be landscape 
         if (optimalSize.width < optimalSize.height || usingLandscape) 
          parameters.setPictureSize(optimalSize.height, optimalSize.width); 
         else 
          parameters.setPictureSize(optimalSize.width, optimalSize.height); 
        } 
        else 
        { 
         // If the device is in portraint and width > height, 
         // or if the device is in landscape and height > height, so we need to rotate them. 
         switch (context.getResources().getConfiguration().orientation) { 
         case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : 
          if (optimalSize.height > optimalSize.width) { 
           parameters.setRotation(camId == GBCameraUtil.findFrontFacingCamera() ? 270 : 90); 
          } 

          break;        
         case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT :      
          if (optimalSize.width > optimalSize.height) { 
           parameters.setRotation(camId == GBCameraUtil.findFrontFacingCamera() ? 270 : 90); 
          } 

          break;        
         } 

         parameters.setPictureSize (optimalSize.width, optimalSize.height); 
        } 
        Log.d("camera","set param"); 
        myCamera.setParameters(parameters); 
        Log.d("camera","set preview disp"); 

        myCamera.setPreviewDisplay(holder); 
        Log.d("camera","start preview"); 
        myCamera.startPreview(); 
        Log.d("camera","take picture"); 
        myCamera.takePicture(null, null, getJpegCallback()); 


       } catch (Exception e) { 
        // Sorry, nothing to do 
       } 
      } 
     } 
    } 

    private PictureCallback getJpegCallback(){ 
     PictureCallback jpeg=new PictureCallback() { 
      @Override 
      public void onPictureTaken(byte[] data, Camera camera) { 
       myCamera.stopPreview(); 
       mPreviewRunning = false; 
       Log.d("camera","pic taken"); 

       if (data != null) { 
        Toast.makeText(context, "data not null ", Toast.LENGTH_LONG).show(); 
        Log.d("camera","data not null. Filename is :" + fileName); 


        FileOutputStream fos; 
        try { 
         if (fileName.equals("")) { 
          SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss"); 
          String date = dateFormat.format(new Date()); 
          if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 
           fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "picture" + date + ".jpg"; 
          } 
          else{ 
           Toast.makeText(context, "storing to internal " , Toast.LENGTH_LONG).show(); 
           // fileName = Environment.getDataDirectory().getAbsolutePath() + "/" + "picture" + date + ".jpg"; 
           fileName = context.getFilesDir().getAbsolutePath()+ "/" + "unlocked" + date + ".jpg"; 
          } 
          Toast.makeText(context, "filename " + fileName, Toast.LENGTH_LONG).show(); 
          Log.d("camera","filename is : " + fileName); 
         } 
         Log.d("camera","coming here"); 
         fos = new FileOutputStream(new File(fileName)); 
         Log.d("camera","writing data"); 

         fos.write(data); 
         Log.d("camera","written data"); 
         fos.close(); 
        } catch (IOException e) { 
         //do something about it 
         Log.d("camera","some exception"+ e); 
        } 


       } 
       else 
        Toast.makeText(context, "data null " , Toast.LENGTH_LONG).show(); 

       myCamera.release(); 
       myCamera = null;  
      } 
     }; 

     return jpeg; 
    } 
+0

這似乎與http://stackoverflow.com/questions/5946837/android-raw-image-callback-supported-devices – linakis

+0

沒有..其不..其他一個是原始數據回調..但在我的jpeg數據回調函數是返回null的函數。 – user2349990

回答

0

也許問題是,你要求支持預覽尺寸列表。 HTC One X可能支持的圖片大小不同嗎?這不是這裏的問題。看另一個答案。

+0

我甚至嘗試使用getSupportedPictureSize獲取支持的圖片大小,然後將輸出分配給setPictureSize。即使這樣做也給了我相同的空數據。 – user2349990

+0

好的,你最後要求什麼圖片尺寸? –

+0

320x480 ..我猜這個問題是單獨使用HTC手機。我只是開始了一個照相機意圖拍照。我看到,我點擊相機按鈕後,它拍攝的照片,但不會返回到調用活動,直到用戶點擊後點擊「打勾」標記「拍照」按鈕。由於我在拍攝照片後等待回調,因此無法獲取所需的數據。在有自動觸發「勾號」的方法之前,數據將主要返回爲空。 – user2349990

0

我注意到你的onPictureTaken()camera.stopPreview()。據我所知,它不應該。

參見Camera.takePicture

此方法纔有效時預覽是活動的(startPreview()之後)。 拍攝圖像後預覽將停止;如果要重新開始預覽或拍攝更多照片,來電者必須再次致電startPreview()。這不應該在start()stop()之間調用。

加粗是我的。撥打camera.takePicture()後,您不應致電camera.stopPreview()。該系統將爲你做到這一點。發生這種情況時,有些設備可能會原諒一個意外的呼叫,但HTC One X不會。

+0

刪除了stopPreview ..即使這樣,返回的數據也是空的。 – user2349990