2012-06-28 49 views
0
package map.demo; 
public class MapDemoActivity extends Activity { 

     Button capture; 
     ImageView image; 
     int cameracode=100; 
     Bitmap bm; 
     Boolean result; 
     FileOutputStream fos; 
     File sd; 

     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      capture=(Button)findViewById(R.id.capture); 
      capture.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 

        image=(ImageView)findViewById(R.id.image); 
        Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        startActivityForResult(i, cameracode); 
       } 
      }); 

     } 
     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      // TODO Auto-generated method stub 



         if(requestCode==100) 
         { 



          bm=(Bitmap) data.getExtras().get("data"); 

          image.setImageBitmap(bm); 


          image.setDrawingCacheEnabled(true); 
          bm = image.getDrawingCache(); 

           if(bm==null) 
           { 
            Toast.makeText(getApplicationContext(), "Image is null", 1000).show(); 
           } 
           else 
           { 
            try { 

             fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "image.jpg")); 

             result=bm.compress(CompressFormat.JPEG, 75, fos); 

             fos.flush(); 
             fos.close(); 

            } catch (FileNotFoundException e) { 
             // TODO Auto-generated catch block 

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

             e.printStackTrace(); 
            } 

          }  
         } 

      super.onActivityResult(requestCode, resultCode, data); 
     } 
    } 

我做上面的代碼,以捕捉攝像機圖像,並設置圖像圖像視圖 &轉換圖像JPEG, 但我沒有得到的圖像,它顯示了空。即在圖像捕獲後我的代碼bm = null。 但圖像視圖顯示圖像,這是默認情況下相機(我正在使用模擬器捕捉圖像)。相機圖像轉換

回答

0

您沒有得到圖像,因爲您沒有提供任何圖像URI來保存圖像。請點擊this鏈接。

0

您好我可能失去了一些東西,但你有沒有代碼用於啓動相機simular這樣:

public CameraView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    // We're implementing the Callback interface and want to get notified 
    // about certain surface events. 
    getHolder().addCallback(this); 
    // We're changing the surface to a PUSH surface, meaning we're receiving 
    // all buffer data from another component - the camera, in this case. 
    getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
} 

public void surfaceCreated(SurfaceHolder holder) { 
    // Once the surface is created, simply open a handle to the camera hardware. 
    //camera = Camera.open(); 
    try { 
     camera = Camera.open(); 
     camera.setPreviewDisplay(holder); 
    } catch(IOException e) { 
     e.printStackTrace(); 
    } 
} 

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
    // This method is called when the surface changes, e.g. when it's size is set. 
    // We use the opportunity to initialize the camera preview display dimensions. 
    Log.d(TAG, "surfaceChanged: " + width + "x" + height); 
    Camera.Parameters p = camera.getParameters(); 
    //p.set("orientation", "portrait"); 
    int w = p.getPreviewSize().width; 
    int h = p.getPreviewSize().height; 
    p.setPreviewSize(w, h);  
    camera.setParameters(p); 

    // ...and start previewing. From now on, the camera keeps pushing preview 
    // images to the surface. 
    camera.startPreview(); 
} 

,我以爲你也這樣設置清單中的權限:

<uses-permission android:name="android.permission.CAMERA"/> 

另請注意,如果攝像機設置正確,仿真器將只顯示棋盤圖案。如果長寬比設置正確,矩形必須是正方形(矩形意味着您有失真的圖像)。

0

我想這裏的問題是,您沒有提供完全繪製ImageView的時間,而是您甚至在它出現之前嘗試獲取緩存。所以你總是最終變爲空,並且你的Toast被顯示出來。相反,這是一種方法。看看這個,

刪除此行,看看它是如何去。

bm = image.getDrawingCache(); 

原因是您已將Captured Image保存到此Bitmap對象並將其設置爲ImageView。那麼爲什麼你依靠ImageView再次獲取位圖對象。如果要縮小圖像的大小,可以使用位圖類中的createScaledBitamp方法。

+0

感謝它的工作....偉大的工作 – user1461473

+0

很高興聽到..所以如何接受這作爲答案然後.. :) –