以原始格式保存图像,而无需压缩 [英] Save image in original form and without compressing

查看:86
本文介绍了以原始格式保存图像,而无需压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存图像而不对其进行压缩,并希望以其原始形式保存它,即,当图像被压缩时,图像尺寸减小并且图像变小.我正在为此目的使用本机相机.我在 android studio 中工作.我正在制作一个使用相机保存图像的应用程序.但是图像是压缩的,非常小,质量也很低.

I want to save the image without compressing it and want to save it in it's original form i.e. when image is compressed the image size decreases and image become small. I am using native camera for this purpose. I am working in android studio. I am making an app in which i am using a camera to save image. But the image is compressed and is very small and very lower quality.

下面是我保存图像的代码

Below is my code for saving image

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

    final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
    if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
    {
        if(resultCode == Activity.RESULT_OK)
        {
            Bitmap bmp = (Bitmap)data.getExtras().get("data");
            ByteArrayOutputStream stream = new ByteArrayOutputStream();

            bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            byte[] byteArray = stream.toByteArray();

            // convert byte array to Bitmap
            Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

            if(isStoragePermissionGranted())
            {
                SaveImage(bitmap);
            }


        }
    }else {
        switch (requestCode) {
            case 1000:
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        // All required changes were successfully made
                        getLocation();
                        break;
                    case Activity.RESULT_CANCELED:
                        // The user was asked to change settings, but chose not to
                        Toast.makeText(this, "Location Service not Enabled", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
                break;
        }

    }


}

private void SaveImage(Bitmap finalBitmap) {

    File storageDir = new File (String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)));
    String root = storageDir + Environment.getExternalStorageDirectory().getAbsolutePath().toString();
    Log.v(LOG_TAG, root);
    File myDir = new File(root + "/captured_images");
    if (!myDir .exists())
    {
        myDir .mkdirs();
    }
    Random generator = new Random();
    int n = 1000;
    n = generator.nextInt(n);
    String fname = "Image-" + n + ".jpg";
    File file = new File(myDir,fname);
    try {
        file.createNewFile();
        MediaScannerConnection.scanFile(getApplicationContext(), new String[]  {file.getPath()} , new String[]{"image/*"}, null);
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG,100,out);
        out.flush();
        out.close();

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

}

更新1

为了获得更多理解,我将使用 onClick 方法

For more understanding i am putting my onClick method

   btn_camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Check permission for camera

            if(ActivityCompat.checkSelfPermission(MainActivity.this, CAMERA)
                    != PackageManager.PERMISSION_GRANTED)
            {
                // Check Permissions Now
                // Callback onRequestPermissionsResult interceptado na Activity MainActivity

                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{CAMERA},
                        MainActivity.REQUEST_CAMERA);
            }
            else {


                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE );

            }


        }
    });

我搜索了解决方案,发现我可以使用 AndroidBitmapUtil类,因此我将其复制并粘贴进入我的项目,即我创建了一个新类.但是我不知道如何使用它.尽管描述了我可以通过执行 new AndroidBmpUtil().save(bmImage,file); 保存图像.但是我又无法与我的代码匹配:(

I searched the solution and found that i can use AndroidBitmapUtil class so i copy and pasted this class into my project i.e. i created a new class. But i don't know how to use it. Although it is described that i can save image by doing new AndroidBmpUtil().save(bmImage, file);. But again i can't match with my code :(

任何帮助将不胜感激

推荐答案

用于打开相机使用

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 File f = new File(android.os.Environment.getExternalStorageDirectory(), "imagename.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);

并在您的onActivityresult方法中使用

and in your onActivityresult method use

if (requestCode == 1) {
 File f = new File(Environment.getExternalStorageDirectory().toString());
  for (File temp : f.listFiles()) {
   if (temp.getName().equals("imagename.jpg")) {
    f = temp;
     break;
  }
  }
  try {
   Bitmap bitmap;
   BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

       bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),bitmapOptions);

      yourimageview.setImageBitmap(bitmap);

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

这篇关于以原始格式保存图像,而无需压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆