1

我现在在我的应用程序中使用相机 API,我在应用程序中有两个问题:

  1. 我正在将图像保存到 sdcard 中的文件夹中,问题是在捕获并查看缩略图显示正确图像的文件夹后,因此在选择照片后它将全屏显示以前拍摄的照片。每次我以相同的名称保存照片所以每次它都会覆盖以前的图像。

  2. 第二个问题是我正在使用params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH)当我按下返回按钮时闪光灯没有打开它仍然保持打开状态。我已经尝试camera.release()surfaceDestroyed(SurfaceHolder holder)

4

1 回答 1

0

你可以尝试这样的事情,但只是一个粗略的代码给你方向。

Bitmap bmp;
public void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);

   //fullscreen 
   requestWindowFeature(Window.FEATURE_NO_TITLE);
   getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

   //set view
   setContentView(R.layout.main);

   preview = new Preview(this); // <3>
   rootLayout = ((FrameLayout) findViewById(R.id.preview));
   rootLayout.addView(preview);
   progress = new ProgressDialog(this);
   ImageView rectView = (ImageView) findViewById(R.id.imageView1);
   rectView.bringToFront();

   buttonClick = (Button) findViewById(R.id.buttonClick);
   buttonClick.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) { // <5>
           new connectAsyncTask.execute();
           buttonClick.setEnabled(false);
       }
   });
}
public void comparison(byte[] data,Camera camera) throws IOException
{
   String path=getString(R.string._sdcard_path);//<string name="_sdcard_path">/sdcard/images/</string> 
   String extension=getString(R.string._sdcard_d_jpg);// <string name="_sdcard_d_jpg">.jpg</string>
   /*String srcPath1="/sdcard/images/";
   File f=new File(srcPath1);
   int h=0,count=0;
   String [] cnt=f.list();
       while(h<cnt.length)
       {
           count++;

           h++;
       }extension=count+extension;*/
   ByteArrayOutputStream stream=null;
   FileOutputStream outStream = null;

  bmp =BitmapFactory.decodeByteArray(data, 0, data.length);


  // int i=0,x=50;

       stream=new ByteArrayOutputStream();

       temBitmap.compress(Bitmap.CompressFormat.JPEG,100, stream);
       path+=String.format(
               getString(R.string._sdcard_d_jpg),
               System.currentTimeMillis());
       outStream = new FileOutputStream(path+extension); // <9>
       outStream.write(stream.toByteArray());
       outStream.close();      
           //rs="Place the strip correctly!!";
           Toast.makeText(getApplicationContext(),"Test", Toast.LENGTH_LONG).show();



}

// Called when shutter is opened
ShutterCallback shutterCallback = new ShutterCallback() { // <6>
   @Override
   public void onShutter() {
       Log.d(TAG, "onShutter'd");
   }
};

// Handles data for raw picture
PictureCallback rawCallback = new PictureCallback() { // <7>
   @Override
   public void onPictureTaken(byte[] data, Camera camera) {
       Log.d(TAG, "onPictureTaken - raw");
   }
};

// Handles data for jpeg picture
PictureCallback jpegCallback = new PictureCallback() { // <8>
   @Override
   public void onPictureTaken(byte[] data, Camera camera) {
       try {
           comparison(data,camera);//new connectAsyncTask(data, camera).execute();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }

   }
};



void showProgress() {
   //Toast.makeText(getApplicationContext()," Picture is saving please wait..!!", Toast.LENGTH_SHORT).show();
   progress.setIndeterminate(true);
   progress.setCancelable(false);
   progress.setCanceledOnTouchOutside(false);
   progress.show();

}


private class connectAsyncTask extends AsyncTask<Void, Void, String> {
   // private ProgressDialog progressDialog;

   connectAsyncTask(byte[] data, Camera camera) throws IOException {
       //saveImageToSd(data, camera);

       //Toast.makeText(getApplicationContext(), stat, Toast.LENGTH_LONG).show();
   }

   @Override
   protected void onPreExecute() {
       // TODO Auto-generated method stub
       super.onPreExecute();
       showProgress();
   }

   @Override
   protected String doInBackground(Void... params) {

       preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
       comparison(data,camera);
       return "";
   }

   @Override
   protected void onPostExecute(String result) {
       super.onPostExecute(result);
       progress.dismiss();
       ImageView rectView = (ImageView) findViewById(R.id.imageView1);
       rectView.setImageBitmap(bmp);

   }

}
@Override
protected void onDestroy() {
    Parameters params = camera.getParameters();

       params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
       camera.setParameters(params);
       if(camera!=null){
           camera.stopPreview();
           camera.release();
               camera = null;
       }
}
于 2013-05-21T13:47:01.043 回答