Camera picture to Bitmap results in messed up image

后端 未结 7 1869
谎友^
谎友^ 2020-12-29 10:00

This is my code that I use to save the image to a Bitmap. This piece of code is based on the code from CyanogenMod\'s camera app so I would assume it would work

相关标签:
7条回答
  • 2020-12-29 10:05

    Try this

    Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
    imv.setImageBitmap(bmp);
    
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bmp.compress(CompressFormat.JPEG, 70, bos);
    
    0 讨论(0)
  • 2020-12-29 10:15

    Did you try to put null at your Options? Try that:

    @Override
    public void onPictureTaken(final byte[] data, Camera camera) {
        Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length, null);
        try {
            FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/image.jpg");
            bm.compress(Bitmap.CompressFormat.JPEG, 95, out);
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    0 讨论(0)
  • 2020-12-29 10:20

    I would suggest not setting the picture size in code and let the camera return the best picture it can capture. Check the code given in the answer here. https://stackoverflow.com/a/9745780/2107118

    0 讨论(0)
  • 2020-12-29 10:21

    The height and width aren't supported for that camera. That's why it looks like that and why the 1280x720 setting works. It's the same as if you set your starcraft to run at a resolution not supported by your monitor.

    0 讨论(0)
  • 2020-12-29 10:29

    Are you setting the parameters from the back camera to the front camera? Another thing to test is for the front camera, first try not setting any parameter, just use the default settings to see if it will work. I ran into similar problems on Samsung devices but that was for back camera. Also you may need to rotate the images taken with front camera(for Samsung devices back camera needs that too)

    public static Bitmap rotate(Bitmap bitmap, int degree) {
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();
    
            Matrix mtx = new Matrix();
            mtx.postRotate(degree);
    
            return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
        }
    
    0 讨论(0)
  • 2020-12-29 10:30

    I should probably also mention that saving the data directly using a FileOutputStream was creating a proper JPEG image. So the problem must be with the BitmapFactory and the way I create the Bitmap.

    Camera outputs fully processed JPEG data (hopefully), and you can write it to disk. Now what you should try:

    1. dump file to disk.
    2. Check that file using a validation tool, or some Image editor on PC.
    3. If Image Editor opens image fine, save a copy from that Image Editor, using standard JPEG profile.
    4. Have BitmapFactory process the original file and file saved by Image editor, see if you get a bad bitmap in both.

    If image is valid JPEG and BitmapFactory still produces wrong Bitmap, the problem is with BitmapFactory (less likely), if file output by Camera is invalid JPEG, problem is with Camera (more likely).

    0 讨论(0)
提交回复
热议问题