尝试从图像创建位图

时间:2019-04-28 23:07:00

标签: java android android-bitmap android-camera2

我正在尝试制作一个可以拍照并从中获取数据的应用程序。我一直在基于this tutorial进行工作,但尝试对其进行修改,以使图像被转换为​​位图而不是被保存。主要问题是当我尝试使用imageView显示该位图时,应用程序崩溃了,这使我相信它没有正确转换为位图。

我尝试改用Intent,但是我需要在应用程序中拍照,因为拍照时屏幕上必须有网格覆盖。意图似乎只是打开相机应用程序。

            ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
                @Override
                public void onImageAvailable(ImageReader imageReader) {
                    Image image = null;
                    try{
                        image = reader.acquireLatestImage();
                        ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                        byte[] bytes = new byte[buffer.capacity()];
                        buffer.get(bytes);
                        save(bytes);

                    }
                    catch (FileNotFoundException e)
                    {
                        e.printStackTrace();
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                    finally {
                        {
                            if(image != null)
                                image.close();
                        }
                    }
                }
                private void save(byte[] bytes) throws IOException {
                    Bitmap bitmap = Bitmap.createBitmap(515, 515, Bitmap.Config.ARGB_8888);
                    //bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(bytes));
                    bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                    switch (imageCount) {
                        case 0:
                            picture1 = bitmap;
                            previousImage.setImageBitmap(picture1);
                            imageCount++;
                            break;
                        case 1:
                            picture2 = bitmap;
                            previousImage.setImageBitmap(picture2);
                            imageCount++;
                            break;
                        case 2:
                            picture3 = bitmap;
                            previousImage.setImageBitmap(picture3);
                            imageCount++;
                            break;
                        case 3:
                            picture4 = bitmap;
                            previousImage.setImageBitmap(picture4);
                            imageCount++;
                            break;
                        case 4:
                            picture5 = bitmap;
                            previousImage.setImageBitmap(picture5);
                            imageCount++;
                            break;
                        case 5:
                            picture6 = bitmap;
                            previousImage.setImageBitmap(picture6);
                            imageCount++;
                            break;
                        case 6:
                            System.out.println("Cannot take anymore pictures");
                        default:
                            System.out.println("Something went wrong ¯ ∖_( ツ )_/ ¯");
                    }
                }

            };

这是崩溃日志:

E/AndroidRuntime: FATAL EXCEPTION: camera Background
    Process: com.example.colmet, PID: 13608
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:8182)
        at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1271)
        at android.view.View.requestLayout(View.java:20105)
        at android.view.View.requestLayout(View.java:20105)
        at android.view.View.requestLayout(View.java:20105)
        at android.view.View.requestLayout(View.java:20105)
        at android.view.View.requestLayout(View.java:20105)
        at android.view.View.requestLayout(View.java:20105)
        at android.support.constraint.ConstraintLayout.requestLayout(ConstraintLayout.java:3172)
        at android.view.View.requestLayout(View.java:20105)
        at android.widget.ImageView.setImageDrawable(ImageView.java:491)
        at android.support.v7.widget.AppCompatImageView.setImageDrawable(AppCompatImageView.java:100)
        at android.widget.ImageView.setImageBitmap(ImageView.java:621)
        at android.support.v7.widget.AppCompatImageView.setImageBitmap(AppCompatImageView.java:108)
        at com.example.colmetAndroid.MainActivity$6.save(MainActivity.java:358)
        at com.example.colmetAndroid.MainActivity$6.onImageAvailable(MainActivity.java:333)
        at android.media.ImageReader$ListenerHandler.handleMessage(ImageReader.java:648)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:158)
        at android.os.HandlerThread.run(HandlerThread.java:61)
I/Process: Sending signal. PID: 13608 SIG: 9
Application terminated.

0 个答案:

没有答案