当前位置: 首页 > 知识库问答 >
问题:

无法使用android摄像头拍摄两次

柴赞
2023-03-14

我按照这个旋转捕获的图像。但我有一个错误。

我的代码

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            //h=0;
            File f = new File(Environment.getExternalStorageDirectory().toString());
            for (File temp : f.listFiles()) {
                if (temp.getName().equals("temp.jpg")) {
                    f = temp;
                    File photo = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
                    //pic = photo;
                    break;
                }
            }

               try {

                Bitmap bitmap;
                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                bitmapOptions.inJustDecodeBounds = false;
                bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
                bitmapOptions.inDither = true;
                bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
                BitmapFactory.Options opts = new BitmapFactory.Options();
                Bitmap bm = BitmapFactory.decodeFile(f.getAbsolutePath(), opts);
                ExifInterface exif = new ExifInterface(f.getAbsolutePath());
                String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
                int orientation = orientString != null ? Integer.parseInt(orientString) :  ExifInterface.ORIENTATION_NORMAL;
                int rotationAngle = 0;
                if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
                if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
                if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;
                Matrix matrix = new Matrix();
                matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
                Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bitmapOptions.outWidth, bitmapOptions.outHeight, matrix, true);

                Global.img = bitmap;

                b.setImageBitmap(bitmap);
                String path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Phoenix" + File.separator + "default";
                //p = path;
                f.delete();
                OutputStream outFile = null;
                File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                try {

                    outFile = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                    //pic=file;
                    outFile.flush();
                    outFile.close();


                } catch (FileNotFoundException e) {
                    e.printStackTrace();

                } catch (IOException e) {
                    e.printStackTrace();

                } catch (Exception e) {
                    e.printStackTrace();
                }

            } catch (Exception e) {
                e.printStackTrace();

            }

        } else if (requestCode == 2) {

            Uri selectedImage = data.getData();
            // h=1;
            //imgui = selectedImage;
            String[] filePath = {MediaStore.Images.Media.DATA};
            Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePath[0]);
            String picturePath = c.getString(columnIndex);
            c.close();
            Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
            Log.w("path of image ******", picturePath + "");
            b.setImageBitmap(thumbnail);
        }


    }
    else
    {
        finish();
    }

}

LogCat错误

  Process: com.example.project.project, PID: 13045
    java.lang.OutOfMemoryError
            at android.graphics.Bitmap.nativeCreate(Native Method)
            at android.graphics.Bitmap.createBitmap(Bitmap.java:928)
            at android.graphics.Bitmap.createBitmap(Bitmap.java:901)
            at android.graphics.Bitmap.createBitmap(Bitmap.java:833)
            at com.example.project.project.ImageFitScreen.onActivityResult(ImageFitScreen.java:236)
            at android.app.Activity.dispatchActivityResult(Activity.java:5643)

这是236号线

 Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bitmapOptions.outWidth, bitmapOptions.outHeight, matrix, true);

我怎样才能解决这个问题?我从中找到了很多解决方案,但我仍然不知道如何解决它。有人能帮我解决这个问题吗?任何帮助都会很好,谢谢!!

共有3个答案

越飞语
2023-03-14

尝试使用这种方式拍摄最多6次的相机图像。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    // if the result is capturing Image
    // int count = 1, countsix = 6;

    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
        if (resultCode == this.RESULT_OK) {

            Log.e("in the On Activity Result", ":::: " + count);
            if (count != countFix) {

                Log.e("in the From Camera if con ", "Count Increment :: "
                        + count);
                count++;
                if (count == 1) {
                    first = mediaFile.toString();
                    cameraAndGalaryPicture();

                    Log.e("in the Count ", "::: 1 " + first);
                    // getBitmapFromURL(first);

                    int targetW = 450;
                    int targetH = 800;
                    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                    bmOptions.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(first, bmOptions);
                    int photoW = bmOptions.outWidth;
                    int photoH = bmOptions.outHeight;
                    // Determine how much to scale down the image
                    int scaleFactor = Math.min(photoW / targetW, photoH
                            / targetH);

                    // Decode the image file into a Bitmap sized to fill the
                    // View

                    // imgHome.setImageBitmap(bitmap);

                    bmOptions.inJustDecodeBounds = false;
                    bmOptions.inSampleSize = scaleFactor << 1;
                    bmOptions.inPurgeable = true;
                    Bitmap bitmap = BitmapFactory.decodeFile(first,
                            bmOptions);

                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    byte[] img1 = stream.toByteArray();
                    String timeStamp = new SimpleDateFormat(
                            "yyyyMMdd_HHmmss", Locale.getDefault())
                            .format(new Date());
                    Parseimagecam1 = new ParseFile("IMG_" + timeStamp
                            + ".jpg", img1);
                    Parseimagecam1.saveInBackground();

                    parseFileCamera.add(0, Parseimagecam1);

                    image_Photo1.setImageBitmap(bitmap);
                    StorePath = first + "," + second + "," + thrid + ","
                            + four + "," + five + "," + six;
                } else if (count == 2) {
                    second = mediaFile.toString();
                    cameraAndGalaryPicture();
                    Log.e("in the Count ", "::: 2 " + second);

                    // getBitmapFromURL(second);

                    int targetW = 450;
                    int targetH = 800;
                    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                    bmOptions.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(second, bmOptions);
                    int photoW = bmOptions.outWidth;
                    int photoH = bmOptions.outHeight;
                    // Determine how much to scale down the image
                    int scaleFactor = Math.min(photoW / targetW, photoH
                            / targetH);

                    // Decode the image file into a Bitmap sized to fill the
                    // View
                    bmOptions.inJustDecodeBounds = false;
                    bmOptions.inSampleSize = scaleFactor << 1;
                    bmOptions.inPurgeable = true;
                    Bitmap bitmap = BitmapFactory.decodeFile(second,
                            bmOptions);

                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    byte[] img2 = stream.toByteArray();
                    String timeStamp = new SimpleDateFormat(
                            "yyyyMMdd_HHmmss", Locale.getDefault())
                            .format(new Date());
                    Parseimagecam2 = new ParseFile("IMG_" + timeStamp
                            + ".jpg", img2);
                    Parseimagecam2.saveInBackground();

                    parseFileCamera.add(1, Parseimagecam2);
                    Log.e("PARSE IMAGE CAMERA 2", ":::: : "
                            + Parseimagecam2);
                    StorePath = first + "," + second + "," + thrid + ","
                            + four + "," + five + "," + six;
                } else if (count == 3) {
                    thrid = mediaFile.toString();
                    cameraAndGalaryPicture();
                    Log.e("in the Count ", "::: 3 " + thrid);

                    int targetW = 450;
                    int targetH = 800;
                    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                    bmOptions.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(thrid, bmOptions);
                    int photoW = bmOptions.outWidth;
                    int photoH = bmOptions.outHeight;
                    // Determine how much to scale down the image
                    int scaleFactor = Math.min(photoW / targetW, photoH
                            / targetH);

                    // Decode the image file into a Bitmap sized to fill the
                    // View
                    bmOptions.inJustDecodeBounds = false;
                    bmOptions.inSampleSize = scaleFactor << 1;
                    bmOptions.inPurgeable = true;
                    Bitmap bitmap = BitmapFactory.decodeFile(thrid,
                            bmOptions);

                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    byte[] img3 = stream.toByteArray();
                    String timeStamp = new SimpleDateFormat(
                            "yyyyMMdd_HHmmss", Locale.getDefault())
                            .format(new Date());
                    Parseimagecam3 = new ParseFile("IMG_" + timeStamp
                            + ".jpg", img3);
                    Parseimagecam3.saveInBackground();

                    // parseFileCamera.add(2, Parseimagecam3);

                    StorePath = first + "," + second + "," + thrid + ","
                            + four + "," + five + "," + six;
                } else if (count == 4) {
                    four = mediaFile.toString();
                    cameraAndGalaryPicture();
                    Log.e("in the Count ", "::: 4 " + four);

                    int targetW = 450;
                    int targetH = 800;
                    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                    bmOptions.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(four, bmOptions);
                    int photoW = bmOptions.outWidth;
                    int photoH = bmOptions.outHeight;
                    // Determine how much to scale down the image
                    int scaleFactor = Math.min(photoW / targetW, photoH
                            / targetH);

                    // Decode the image file into a Bitmap sized to fill the
                    // View
                    bmOptions.inJustDecodeBounds = false;
                    bmOptions.inSampleSize = scaleFactor << 1;
                    bmOptions.inPurgeable = true;
                    Bitmap bitmap = BitmapFactory.decodeFile(four,
                            bmOptions);

                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    byte[] img4 = stream.toByteArray();
                    String timeStamp = new SimpleDateFormat(
                            "yyyyMMdd_HHmmss", Locale.getDefault())
                            .format(new Date());
                    Parseimagecam4 = new ParseFile("IMG_" + timeStamp
                            + ".jpg", img4);
                    Parseimagecam4.saveInBackground();

                    // parseFileCamera.add(3, Parseimagecam4);

                    StorePath = first + "," + second + "," + thrid + ","
                            + four + "," + five + "," + six;
                } else if (count == 5) {
                    five = mediaFile.toString();
                    cameraAndGalaryPicture();
                    Log.e("in the Count ", "::: 5 " + five);

                    int targetW = 450;
                    int targetH = 800;
                    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                    bmOptions.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(five, bmOptions);
                    int photoW = bmOptions.outWidth;
                    int photoH = bmOptions.outHeight;
                    // Determine how much to scale down the image
                    int scaleFactor = Math.min(photoW / targetW, photoH
                            / targetH);

                    // Decode the image file into a Bitmap sized to fill the
                    // View
                    bmOptions.inJustDecodeBounds = false;
                    bmOptions.inSampleSize = scaleFactor << 1;
                    bmOptions.inPurgeable = true;
                    Bitmap bitmap = BitmapFactory.decodeFile(five,
                            bmOptions);

                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    byte[] img5 = stream.toByteArray();
                    String timeStamp = new SimpleDateFormat(
                            "yyyyMMdd_HHmmss", Locale.getDefault())
                            .format(new Date());
                    Parseimagecam5 = new ParseFile("IMG_", img5);
                    // Parseimagecam5.saveInBackground();

                    parseFileCamera.add(4, Parseimagecam5);

                    StorePath = first + "," + second + "," + thrid + ","
                            + four + "," + five + "," + six;
                } else if (count == 6) {
                    six = mediaFile.toString();
                    // cameraAndGalaryPicture();
                    Log.e("in the Count ", "::: 6 " + six);

                    int targetW = 450;
                    int targetH = 800;
                    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                    bmOptions.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(six, bmOptions);
                    int photoW = bmOptions.outWidth;
                    int photoH = bmOptions.outHeight;
                    // Determine how much to scale down the image
                    int scaleFactor = Math.min(photoW / targetW, photoH
                            / targetH);

                    // Decode the image file into a Bitmap sized to fill the
                    // View
                    bmOptions.inJustDecodeBounds = false;
                    bmOptions.inSampleSize = scaleFactor << 1;
                    bmOptions.inPurgeable = true;
                    Bitmap bitmap = BitmapFactory
                            .decodeFile(six, bmOptions);

                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    byte[] img6 = stream.toByteArray();
                    String timeStamp = new SimpleDateFormat(
                            "yyyyMMdd_HHmmss", Locale.getDefault())
                            .format(new Date());
                    Parseimagecam6 = new ParseFile("IMG_" + timeStamp
                            + ".jpg", img6);
                    Parseimagecam6.saveInBackground();

                    parseFileCamera.add(5, Parseimagecam6);

                    StorePath = first + "," + second + "," + thrid + ","
                            + four + "," + five + "," + six;

                }
                Log.e("Store Path ", ":: : " + StorePath);
                Log.e("parseFileCamera",
                        ":: : Size " + parseFileCamera.toString());

                Log.e("Boolean Value Chaeck", ":: : Tru or fls  "
                        + ImageCamera);

                for (int i = 0; i < parseFileCamera.size(); i++) {
                    ParseFile image = (ParseFile) parseFileCamera.get(i);

                    Log.e("in the for loop ", ":::: " + image.getUrl());
                }
            } else {

            }
        } else if (resultCode == this.RESULT_CANCELED) {
            // user cancelled Image capture
            Toast.makeText(getApplicationContext(),
                    "User cancelled image capture", Toast.LENGTH_SHORT)
                    .show();
        } else {
            // failed to capture image
            Toast.makeText(getApplicationContext(),
                    "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                    .show();
        }

    }
}

cameraAndGalaryPicture()这是用于打开相机捕获方法的方法。countFix此变量用于其Fix 6。parseFileCamera这是Parse数组。这是解析文件对象

我在解析数据库时使用它。此外,u将采取字节数组或其他明智的位图数组来存储更多的图像。

司寇书
2023-03-14

我猜你的应用程序是内存密集型的,你的相机图片至少应该有1280x720的大小。我修改了你的代码创建了一个名为

解码位图

  private Bitmap decodeBitmap(String filePath, int reqWidth, int reqHeight, int scale) throws IOException, Exception {
    if (scale > 3)
        throw new Exception("scale size cannot be greater than 3");
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    bmOptions.inSampleSize = scale + Math.min(photoW / reqWidth, photoH / reqHeight);
    bmOptions.inJustDecodeBounds = false;
    try {
        Bitmap bm = BitmapFactory.decodeFile(filePath, bmOptions);
        ExifInterface exif = new ExifInterface(filePath);
        String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;

        int rotationAngle = 0;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;

        Matrix matrix = new Matrix();
        matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
        return Bitmap.createBitmap(bm, 0, 0, photoW, photoH, matrix, true);
    } catch (OutOfMemoryError exception) {
        exception.printStackTrace();
        return decodeBitmap(filePath, reqWidth, reqHeight, scale + 1);
    }
}

onActivityResult

  try {
        mImageView.setImageBitmap(decodeBitmap(mCurrentPhotoPath,1024,1024,0));
    } catch (Exception e) {
        e.printStackTrace();
    }

让我警告你,这个功能的工作原理是降低一些图像质量。希望它能帮助你。。。

仲璞瑜
2023-03-14

你应该让更少的位图进入内存<代码>回收位图当您永远不需要它们时<代码>设置图像位图不正确。在文件系统中存储位图后,只需使用glideUniversal Image Loader加载它。

 类似资料:
  • 使用手机摄像头拍摄图像可以通过使用PhotoIntent然后上传拍摄的图像是成功的,我可以观察到Firebase存储中拍摄的图像,但我无法从Firebase存储加载图像,我只查看占位符,但从图库加载到图像视图并可见。可能是什么问题,我和photoIntent正在使用Picasso库从Firebase存储渲染此图像?如有任何帮助,我们将不胜感激。附加方法从Firebase实时数据库接收图像url,并

  • 表情测试 图片转文字 相框 拍摄图片

  • 摄像头用于采集图像和影像信息,通过模块间的组合完成各种创意活动。 净重量:8.1g 体积:24×24×22mm 参数 分辨率:1280×720 像素大小:3.4um×3.4um 最大图像传输速率:全尺寸 @ 30fps 视场角:100° 镜片结构:4G+IR 焦比:2.97 有效焦距:2.4mm 功耗:100uA(待机)~240mW(工作) 电源:USB总线电源 抗跌落能力:1m 工作温度:-30

  • 主要内容:第1步:加载OpenCV本机库,第2步:实例化视频捕获类,第3步:阅取帧,示例在本章中,我们将学习如何使用OpenCV使用系统摄像头捕获帧。包中的类包含使用相机捕获视频的类和方法。下面来一步一步学习如何捕捉帧 - 第1步:加载OpenCV本机库 在使用OpenCV库编写Java代码时,使用加载OpenCV本地库。加载OpenCV本机库,如下所示 - 第2步:实例化视频捕获类 使用本教程前面提到的函数来实例化类。 第3步:阅取帧 可以使用类的方法从相机读取帧。此方法接受类的对

  • 我有一个按钮,它向用户提供选择,他是否想要从画廊或相机的图像。当用户从图库拍摄图像时,我能够成功地将图像上传到Firebase存储,但是当用户选择相机时,我无法将图像上传到存储。这是用户选择相机时的onActivityResult代码 它将图像上传到我的imageview,但给我错误提示 当我尝试上载到存储时 我在stack overflow上遇到了一个类似的问题。 我认为公认的答案是错误的,因为

  • 拍摄图像或影像 安装PSP™专用的摄影机(选购品)后,可拍摄静止图像或影像。拍摄后的静止图像或影像会被保存至Memory Stick™或主机内存中。使用PSP™摄影机机能时,请遵守一般礼仪适切使用。 提示 拍摄的图像会被保存至Memory Stick™或主机内存中。 初期设定的保存位置为主机内存。若要变更为Memory Stick™,请在操作接口选择(设定)>[保存位置]。   准备 1. 将摄影