【问题标题】:Android: How do i set imageview with photo taken through a custom cameraAndroid:如何使用通过自定义相机拍摄的照片设置 imageview
【发布时间】:2014-04-05 08:40:14
【问题描述】:

我有一个自定义相机应用程序,当我按下捕获按钮时,捕获的图像将作为预览图像出现在下一个活动中。

这是我去过的地方 -

MainActivity.java

public class MainActivity extends Activity {
private Camera mCamera;
private CameraPreview mCameraPreview;
private File pictureFile;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    mCamera = getCameraInstance();
    mCameraPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    final ImageView view = (ImageView) findViewById(R.id.imageView1);
    preview.addView(mCameraPreview);

    Button captureButton = (Button) findViewById(R.id.button_capture);
    captureButton.setOnClickListener(new View.OnClickListener() {
        @SuppressLint("NewApi")
        @Override
        public void onClick(View v) {
            mCamera.takePicture(null, null, mPicture);

        }
    });
}

/**
 * Helper method to access the camera returns null if it cannot get the
 * camera or does not exist
 * 
 * @return
 */
private Camera getCameraInstance() {
    Camera camera = null;
    try {
        camera = Camera.open();
    } catch (Exception e) {
        // cannot get camera or does not exist
    }
    return camera;
}

PictureCallback mPicture = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        pictureFile = getOutputMediaFile();
        if (pictureFile == null) {
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {

        } catch (IOException e) {
        }
    }
};

private static File getOutputMediaFile() {
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "MyCameraApp");
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(new Date());
    File mediaFile;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator
            + "IMG_" + timeStamp + ".jpg");

    return mediaFile;
}

我的 CameraPreview.java

public class CameraPreview extends SurfaceView implements
SurfaceHolder.Callback {
 private SurfaceHolder mSurfaceHolder;
 private Camera mCamera;

 // Constructor that obtains context and camera
 @SuppressWarnings("deprecation")
 public CameraPreview(Context context, Camera camera) {
 super(context);
 this.mCamera = camera;
 this.mSurfaceHolder = this.getHolder();
 this.mSurfaceHolder.addCallback(this);
 this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
 }

 @Override
 public void surfaceCreated(SurfaceHolder surfaceHolder) {
 try {
mCamera.setDisplayOrientation(90);
    mCamera.setPreviewDisplay(surfaceHolder);
    mCamera.startPreview();
  } catch (IOException e) {

   }
   }

   @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    mCamera.stopPreview();
    mCamera.release();
 }

   @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
     int width, int height) {

     try {
     mCamera.setPreviewDisplay(surfaceHolder);
     mCamera.startPreview();
     } catch (Exception e) {
     // intentionally left blank for a test
     }
     }
      }

我的 activity_main.xml 文件

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/container"
android:orientation="vertical" >

 <FrameLayout
    android:id="@+id/Frame"
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:layout_weight="1" >

     <ImageView
         android:id="@+id/imageView1"
         android:layout_width="100dp"
         android:layout_height="100dp"
         android:layout_gravity="center"
         android:src="@drawable/ic_launcher" />

 </FrameLayout>
<FrameLayout
    android:id="@+id/camera_preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" />

<Button
    android:id="@+id/button_capture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Capture" />

到目前为止,我尝试使用 mPicture 甚至转换图片文件来设置我的 Imageview、视图之一的背景资源,但无济于事。但是预览图像会出现在 camerapreview 上。

有经验丰富的程序员知道如何处理这个问题吗?谢谢!

【问题讨论】:

    标签: java android image camera


    【解决方案1】:

    我认为你可以使用 startActivityForResult .. 这个视频可能会有所帮助https://www.youtube.com/watch?v=8yA0vkjREyI&list=PL2F07DBCDCC01493A

    注意:摄像机 tut 持续到第 42 集。

    【讨论】:

      【解决方案2】:

      我只在您的onPictureTaken() 回调中添加了几行代码:

      public void onPictureTaken(byte[] data, Camera camera) {
          pictureFile = getOutputMediaFile();
          if (pictureFile == null) {
              return;
          }
          try {
              FileOutputStream fos = new FileOutputStream(pictureFile);
              fos.write(data);
              fos.close();
      // ---- added to display imageView ---- 
              BitmapFactory.Options scalingOptions = new BitmapFactory.Options();
              scalingOptions.inSampleSize = camera.getParameters().getPictureSize().width / imageView.getMeasuredWidth();
              final Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length, scalingOptions);
              imageView.setImageBitmap(bmp);
              imageView.setVisibility(ImageView.VISIBLE);
      // ---- end of changes to display imageView ---- 
          } catch (FileNotFoundException e) {
      
          } catch (IOException e) {
          }
      }
      

      【讨论】:

      • 抱歉,添加的部分是如何工作的?尤其是这个 imageView.setImageBitmap(bmp); imageView.setVisibility(ImageView.VISIBLE);
      • 这有什么问题?您可能会设置MainActivity 类的私有字段,并在setContentView() 之后对其进行初始化。但你也可以在onPictureTaken() 中写final ImageView view = (ImageView) findViewById(R.id.imageView1)
      • 对!谢谢!顺便说一句,很抱歉打扰您,您知道如何在视图上添加组件后从视图中检索图像吗?这是链接 - stackoverflow.com/questions/24352022/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多