将字节数组转换为位图会产生黑色图像

时间:2019-06-09 14:48:31

标签: android bitmap type-conversion byte

嘿,我想显示自动拍摄的图像。我在onPictureTaken中获得了图像数据,并且字节数组充满了我想像的像素信息,但是当我尝试在imageView组件中显示它们时,我总是得到黑色图像。 (当然,在将其显示在屏幕上之前,我已将其转换为位图)

这是我的代码:

private String errorMessage;
private Camera mCamera;
private Parameters parameters;
private ImageView iv;
private int picturesTaken;

@Override
protected void onCreate(Bundle savedInstanceState)
    {
    // Default initialisierung
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    errorMessage = null;
    iv = (ImageView) findViewById(R.id.iv);
    picturesTaken = 0;

    // Callback, wenn Bild aufgenommen wurde.
    Camera.PictureCallback pic = new Camera.PictureCallback()
        {
        @Override
        public void onPictureTaken(byte[] bytes, Camera camera)
            {
            // Bild probehalber ausgeben
            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            iv.setImageBitmap(bitmap);

            // Kamera wieder freigeben
            camera.release();
            mCamera = null;
            }
        };

    // Check, ob Hardware ansprechbar ist
    if (this.checkCameraHardware(this))
        {
        // Kameraobjekt erstellen
        mCamera = this.getCameraInstance();
        if (mCamera != null)
            {
            SurfaceTexture view = new SurfaceTexture(10);
            try
                {
                mCamera.setPreviewTexture(view);
                parameters = mCamera.getParameters();
                mCamera.setParameters(parameters);
                mCamera.startPreview();
                mCamera.takePicture(null, null, pic);
                picturesTaken++;
                }
            catch (Exception e)
                {
                errorMessage = e.getMessage();
                }
            }
        else
            {
            Toast.makeText(getApplicationContext(), "Error: Can't create instance of camera!", Toast.LENGTH_LONG).show();
            Log.e("kkkk", "Error: Can't create instance of camera!");
            }
        }
    else
        {
        Toast.makeText(getApplicationContext(), "Error: Camera currently not available!", Toast.LENGTH_LONG).show();
        Log.e("kkkk", "Error: Camera currently not available!");
        }
    }

0 个答案:

没有答案