2012-01-23 70 views
0

在我的應用程序中,我需要使用定製相機。屏幕上有兩個按鈕「保留圖像」和「重拍照片」。當我點擊「保留圖像」按鈕時,由攝像頭拍攝的圖像應保存在指定的目錄中。當我點擊「重拍照片」按鈕時,攝像頭應準備好拍攝新照片。我已經搜索了相同的,我有一個堆棧溢出鏈接,我試圖在我的應用程序中實現,但問題是所有的代碼拍照和保存圖片寫在同一類。拍攝的照片將自動保存,無需點擊按鈕。我想改變這一點。我嘗試過不同的方式,但拋出錯誤。在android中定製相機

請幫我...下面給出低於給我的Java代碼

public class CustomCameraDemo extends Activity { 

private SurfaceView preview=null; 
private SurfaceHolder previewHolder=null; 
public Camera camera=null;  
private boolean inPreview=false; 
//ImageView image; 
Bitmap bmp,itembmp; 
static Bitmap mutableBitmap; 
PointF start = new PointF(); 
PointF mid = new PointF(); 
float oldDist = 1f; 
File imageFileName = null; 
File imageFileFolder = null; 
private MediaScannerConnection msConn; 
Display d; int screenhgt,screenwdh;  
ProgressDialog dialog; 
Button save; 
Button retake; 

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

    save=(Button)findViewById(R.id.Save); 
    retake=(Button)findViewById(R.id.Retake); 
    preview=(SurfaceView)findViewById(R.id.surface);  
    previewHolder=preview.getHolder();  
    previewHolder.addCallback(surfaceCallback);  
    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    previewHolder.setFixedSize(getWindow().getWindowManager()  
      .getDefaultDisplay().getWidth(), getWindow().getWindowManager() 
      .getDefaultDisplay().getHeight()); 
    save.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 



     } 
    }); 
    retake.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 


     } 
    }); 

} 

@Override public void onResume() { 
    super.onResume(); 
    camera=Camera.open(); 
    } 
@Override public void onPause() { 
    if (inPreview) { 
     camera.stopPreview(); } 
    camera.release(); 
    camera=null; 
    inPreview=false; 
    super.onPause(); 
    }  

private Camera.Size getBestPreviewSize(int width, int height,Camera.Parameters parameters){ 
    Camera.Size result=null; 
    for (Camera.Size size : parameters.getSupportedPreviewSizes()) 
    { 
     if (size.width<=width && size.height<=height) 
     { 
      if (result==null) { 
       result=size; 
      } else { 
       int resultArea=result.width*result.height; 
       int newArea=size.width*size.height; 
       if (newArea>resultArea) { 
        result=size; 
        } 
       } 
      } 
     } 
    return(result); 
    }  
    SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback(){ 

    public void surfaceCreated(SurfaceHolder holder) {  
     try {   
      camera.setPreviewDisplay(previewHolder); 
      } catch (Throwable t) { 
       Log.e("PreviewDemo-surfaceCallback", 
         "Exception in setPreviewDisplay()", t); 
       Toast.makeText(CustomCameraDemo.this, t.getMessage(), Toast.LENGTH_LONG).show();  
       }  
      }  
    public void surfaceChanged(SurfaceHolder holder,int format, int width,int height) { 
     Camera.Parameters parameters=camera.getParameters();  
     Camera.Size size=getBestPreviewSize(width, height,           
       parameters);  
     if (size!=null) {  
      parameters.setPreviewSize(size.width, size.height); 
      camera.setParameters(parameters);  
      camera.startPreview();  
      inPreview=true;  
      }  
     }  
    public void surfaceDestroyed(SurfaceHolder holder) { 

     } 
    };   

    Camera.PictureCallback photoCallback=new Camera.PictureCallback(){  

    public void onPictureTaken(final byte[] data, final Camera camera){  
       dialog=ProgressDialog.show(CustomCameraDemo.this,"","Saving Photo");  
       new Thread(){  
        public void run(){  
         try{  
          Thread.sleep(1000); 
          }  
         catch(Exception ex){}  
         onPictureTake(data,camera); 
         }  
        }.start();    
        }  
      };   
    public void onPictureTake(byte[] data, Camera camera){   
       bmp = BitmapFactory.decodeByteArray(data, 0, data.length);  
       mutableBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);  
       savePhoto(mutableBitmap);  
       dialog.dismiss();  
       }   
    class SavePhotoTask extends AsyncTask<byte[], String, String> { 
       @Override  
       protected String doInBackground(byte[]... jpeg) { 
        File photo=new File(Environment.getExternalStorageDirectory(),"photo.jpg"); 
        if (photo.exists()){ 
         photo.delete(); 
         }  try {  
          FileOutputStream fos=new FileOutputStream(photo.getPath()); 
          fos.write(jpeg[0]);  
          fos.close();  
          } catch (java.io.IOException e) { 
           Log.e("PictureDemo", "Exception in photoCallback", e); 
           }  
          return(null); 
           }  
       } 
    public void savePhoto(Bitmap bmp) { 
       imageFileFolder = new File(Environment.getExternalStorageDirectory(),"MyMedicalRecords"); 
       imageFileFolder.mkdir(); 
       FileOutputStream out = null; 
       Calendar c = Calendar.getInstance(); 
       String date = fromInt(c.get(Calendar.MONTH))+ fromInt(c.get(Calendar.DAY_OF_MONTH)) 
       + fromInt(c.get(Calendar.YEAR))    
       + fromInt(c.get(Calendar.HOUR_OF_DAY))    
       + fromInt(c.get(Calendar.MINUTE))   
       + fromInt(c.get(Calendar.SECOND)); 
       imageFileName = new File(imageFileFolder, date.toString() + ".jpg"); 
       try { out = new FileOutputStream(imageFileName); 
       bmp.compress(Bitmap.CompressFormat.JPEG, 100, out); 
       out.flush(); out.close(); 
       scanPhoto(imageFileName.toString()); 
       out = null; 
       } catch (Exception e) { e.printStackTrace(); } 
       } 
    public String fromInt(int val) { 
        return String.valueOf(val); 
        } 
    public void scanPhoto(final String imageFileName) { 
        msConn = new MediaScannerConnection(CustomCameraDemo.this,new MediaScannerConnectionClient() { 
    public void onMediaScannerConnected() { 
          msConn.scanFile(imageFileName, null); 
          Log.i("msClient obj in Photo Utility","connection established"); 
          } 
    public void onScanCompleted(String path, Uri uri) { 
           msConn.disconnect(); Log.i("msClient obj in Photo Utility","scan completed"); 
           } 
          }); 
        msConn.connect(); 
        } 
       @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event){ if ( 
         keyCode == KeyEvent.KEYCODE_MENU&& event.getRepeatCount() == 0) 
       { 
        onBack(); 
        } 
       return super.onKeyDown(keyCode, event); 
       } 
    public void onBack(){ Log.e("onBack :","yes"); 
       camera.takePicture(null,null,photoCallback); 
       inPreview=false; 
    } 


} 

是我的主要佈局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<android.view.SurfaceView 
android:id="@+id/surface" 
    android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
    /> 

<Button android:layout_width="150dip" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true" 
android:text="Keep Photo" 
android:id="@+id/Save"></Button> 
<TextView android:layout_below="@+id/surface" 
android:layout_width="wrap_content" 
android:id="@+id/textView1" 
    android:layout_height="wrap_content" 
android:text="" 
    android:layout_alignParentBottom="true" 
android:layout_centerHorizontal="true"></TextView> 
<Button android:layout_width="150dip" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_toRightOf="@+id/textView1" 
android:text="Retake Photo" 
android:id="@+id/Retake"></Button> </RelativeLayout> 
+0

我沒有得到正確的答案,那麼我該如何接受呢? :( –

+0

試試這個鏈接http://stackoverflow.com/questions/8543244/custom-camera-android/8543376#8543376 – surendra

+0

我在我的應用程序中實現了相同的...但我不能做按鈕點擊保存任務..那是我的問題 –

回答

0

如果你想建立在現有的代碼,你可以直接刪除當用戶選擇重新拍攝其他圖像時保存的圖像。