如何使用Android camera2确认捕获的结果?

时间:2018-11-04 06:31:19

标签: android android-camera2

到目前为止,我使用此tutorial来创建相机活动及其工作正常,但是我需要显示已捕获/结果的图像,并带有重复进行捕获,确认和保存图像或取消的选项。

我搜索了很多Google,但找不到或不知道该怎么做。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

  1. 在布局相机预览中。添加“图像”视图小部件,然后将其设置为“消失”。
  2. 在您的方法ImageReader.OnImageAvailableListener中,byte []结果实现方法将该字节保存为JPEG然后显示。这是我的功能:

    / **

    • 一种将图像保存到存储的方法 * / 私人void saveImageFile(final byte [] bytes)引发IOException { OutputStream outputStream = null;

      尝试{     outputStream =新的FileOutputStream(file);     outputStream.write(bytes);

      //to tell system update data
      sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
      
      //check if dir after create or not
      if (!dir.exists())
          dir.mkdir();
      
      //compress file is file to upload database, so i remove this after get byte
      compressFIle = new File(dir,UUID.randomUUID()+"compresss.jpg");
      compressFIle =  new Compressor(this)
              .compressToFile(file);
      if (latitude != null) {
      
          //change UI
      
          runOnUiThread(new Runnable() {
      
              @Override
              public void run() {
      
                  // Stuff that updates the UI
      
                  imageview.setImage(ImageSource.uri(Uri.fromFile(compressFIle)));
                  //imageview.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP);
                  //imageview.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
                  imageview.setVisibility(View.VISIBLE);
      
                  //replace UI
                  btn_shot.setText("SAVE");
                  btn_galery.setText("Re-shoot");
      
                  try {
                      //call method for savemeta data
                      saveMetaData(file);
                      saveMetaData(compressFIle);
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
      
                  //action for call method save image to storage
                  btn_shot.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
                          //saveImageFile
                          try {
                              bytesfile = FileUtils.readFileToByteArray(compressFIle);
                          } catch (IOException e) {
                              e.printStackTrace();
                          }
      
                          //call method for save data into sqlite
                          saveIntoDatabasesqlite(latitude,longitude,bytesfile,1);//work id default
                          showSnackBar("Saved ", Snackbar.LENGTH_SHORT);
                          new Handler().postDelayed(new Runnable() {
                              @Override
                              public void run() {
      
                                  //delete not compress image
                                  deleteImage(file.getPath());
      
                                  Intent intent = new Intent();
                                  intent.putExtra("photoUri",""+Uri.fromFile(compressFIle));
                                  setResult(RESULT_OK,intent);
                                  finish();
                              }
                          }, 1000);
      
                      }
                  });
                  //action galert Reshot
                  btn_galery.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
                          imageview.setVisibility(View.GONE);
                          unlockFocus();
                          /*deleteImage(file.getPath());*/
                          File newfile = new File(dir,UUID.randomUUID()+".jpg");
                          OutputStream outputStream1 = null;
                          try {
      
                              byte[] newbyte = FileUtils.readFileToByteArray(file);
                              outputStream1 = new FileOutputStream(newfile);
                              outputStream1.write(newbyte);
                              //to tell system update data
                              sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(newfile)));
                          } catch (FileNotFoundException e) {
                              e.printStackTrace();
                          } catch (IOException e) {
                              e.printStackTrace();
                          }finally {
                              finger_spacing = 0;
      
      
                              Log.e("finger spacing reshot",""+finger_spacing);
                              mPreviewRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, null);
                              zoom_level = 1;
      
                              try {
                                  mCaptureSession
                                          .setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback, null);
      
      
      
      
      
      
                              } catch (CameraAccessException e) {
                                  e.printStackTrace();
                              } catch (NullPointerException ex) {
                                  ex.printStackTrace();
                              }
                              if (outputStream1 != null) {
                                  try {
                                      outputStream1.close();
                                  } catch (IOException e) {
                                      e.printStackTrace();
                                  }
                              }
      
      
                          }
      
      
      
      
      
                          //reshot
                          btn_shot.setText("SHOOT");
                          btn_shot.setOnClickListener(new View.OnClickListener() {
                              @Override
                              public void onClick(View view) {
                                  Log.e("fab Click",""+"FAB CLICK reshot");
                                  //take picture
                                  lockFocus();
                              }
                          });
      
                          // action galery
                          btn_galery.setText("Gallery");
                          btn_galery.setOnClickListener(new View.OnClickListener() {
                              @Override
                              public void onClick(View view) {
                                  /*deleteImage(file.getPath());*/
                                  file.delete();
                                  final Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                                  final Uri contentUri = Uri.fromFile(file);
                                  scanIntent.setData(contentUri);
                                  sendBroadcast(scanIntent);
                                  Intent intent = new Intent(ShotActivity_camera2API.this,GaleryActivity.class);
                                  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                  startActivityForResult(intent,GALERY_REQUEST_CODE);
                                  // finish();
                              }
                          });
                      }
                  });
              }
          });
      }
      

      }最后{

      if (outputStream != null)
          outputStream.close();
      

      } }