【问题标题】:android - using byte[] after taking picture in the app itselfandroid - 在应用程序本身拍照后使用 byte[]
【发布时间】:2015-08-08 14:27:54
【问题描述】:

我拍照并想将其放入另一个 Activity 的 ImageView 中,但出现了问题。这是我的代码:

Camera mCamera;

//Some Code

public void onClick(View v) {
    mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);        
}

Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() {
    public void onShutter() {           
    }
};

Camera.PictureCallback rawCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;

        Intent inCapture = new Intent(MainActivity.this, SecondActivity.class);
        //Actually, I don't know where to put it: here or in jpegCallback
        inCapture.putExtra("captured", data);
        startActivity(inCapture);
    }
};

Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {

        //My attempt to load the result into ImageView in the same Activity - didn't work too
        if (data != null) {
            ByteArrayInputStream imageStream = new ByteArrayInputStream(
                    data);
            Bitmap theImage = BitmapFactory
                    .decodeStream(imageStream);
            imageView.setBackground(null);
            imageView.setBackground(new BitmapDrawable(
                    getResources(), theImage));
        }
    }
};

它没有运行 SecondActivity,我不知道如何在 onClick() 中声明我的意图并能够将 byte[] 数据放入其中(不同的类,我不能将意图设为静态 - 至少,我不能't)。有没有更好的方法来捕捉照片并将那个字节 [] 传递给另一个 Activity?

附:我知道还有较新的 Camera2,但到目前为止我不明白如何使用它。

【问题讨论】:

  • “更好的方法”是不使用单独的活动。您不能轻易地将照片传递给另一个活动,因为它对于 Intent 额外的内容来说太大了。改为使用一个活动并单独的片段。
  • @CommonsWare 那么如何将我的位图放在同一个 Activity 的 imageView 中?我试过(将 SurfaceView 设置为 INVISIBLE 并将转换后的 byte[] 作为 Bitmap 设置为 ImageView 但没有结果
  • "那么如何将我的位图放在同一个Activity的imageView中?" -- 在BitmapFactory 上使用decodeByteArray() 得到Bitmap,然后在ImageView 上调用setImageBitmap()。不要使用ByteArrayOutputStream,也不要使用setBackground()
  • @CommonsWare 我做到了,一切都一样,但我在日志中看到“位图太大,无法上传到纹理中(3600x4800,max=4096x4096)。然后我创建了它的 ScaledBitmap(360x480 ),但我得到了同样的信息。
  • "位图太大,无法上传到纹理中 (3600x4800, max=4096x4096)" -- 使用 BitmapFactory.Options 和合适的 inSampleSize 在使用 decodeByteArray() 时对图像进行下采样。例如,尝试inSampleSize of 2。

标签: android camera android-camera android-camera-intent


【解决方案1】:

您可以使用 ContentProvider,它有时可能很有用,尽管需要您自己处理一些实现,但它还不错。您可以做的另一件事是将 Bitmap 对象放入单例类中的队列或 hashmap 中,并通过意图传递该位图的 id,被调用的活动将获取 id 并从那里提取它。你需要在那里处理一些同步。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多