// здесь мой код, я пытаюсь преобразовать изображение в pdf файл, и он даст мне вывод около 300 КБ
public class PdfMainActivity extends AppCompatActivity implements View.OnClickListener {
public static final int GALLERY_PICTURE = 1;
private Button btn_select, btn_convert;
private ImageView iv_image;
private Bitmap bitmap;
private AdView mAdView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf_main);
init();
listener();
// Display ad in your app
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-7657536360406007/7788305544");
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
// Инициализация моей кнопки и изображения
private void init() {
btn_select = findViewById(R.id.btn_select);
btn_convert = findViewById(R.id.btn_convert);
iv_image = findViewById(R.id.iv_image);
}
// Создание кнопок для кнопок
private void listener() {
btn_select.setOnClickListener(this);
btn_convert.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_select:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, GALLERY_PICTURE);
break;
case R.id.btn_convert:
try {
createPdf();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
// Создание pdf для изображений private void createPdf () вызывает IOException {
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(960, 960, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
Paint paint = new Paint();
paint.setColor(Color.parseColor("#ffffff"));
canvas.drawPaint(paint);
bitmap = Bitmap.createScaledBitmap(bitmap, 960, 960, false);
paint.setColor(Color.BLUE);
canvas.drawBitmap(bitmap, 0, 0, null);
document.finishPage(page);
// create file
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_pdf");
if (!myDir.exists()) {
myDir.mkdirs();
}
Random generator = new Random();
// set no of value
int n = 10000;
// next no
n = generator.nextInt(n);
// file name generated
String fname = "Bihar" + n + ".pdf";
// generate pdf file
File file = new File(myDir, fname);
// if file exists
if (file.exists())
//file deleted
file.delete();
// try and catch
try {
// File output stream
FileOutputStream fos = new FileOutputStream(file);
// iv_image.compres(Bitmap.CompressFormat.PNG, 100, fos);
// write to
document.writeTo(fos);
// flush
out.flush();
// close
out.close();
// display a toast successful upload
Toast.makeText(PdfMainActivity.this, "Successful Uploaded", Toast.LENGTH_LONG).show();
//catch
} catch (IOException e) {
e.printStackTrace();
// toast
Toast.makeText(this, "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show();
}
// close the document
document.close();
}
// Средство выбора галереи для выбора изображения
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// gallery picture
if (requestCode == GALLERY_PICTURE && resultCode == RESULT_OK) {
// result code
if (resultCode == RESULT_OK) {
// image selected
Uri selectedImage = data.getData();
// filePathColumn data
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// cursor
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
// move to first
cursor.moveToFirst();
// column index
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
// filepath
String filePath = cursor.getString(columnIndex);
// cursor close
cursor.close();
// shrink bitmap
bitmap = ShrinkBitmap(filePath, 500, 500);
// image set in bitmap
iv_image.setImageBitmap(bitmap);
// btn_convert
btn_convert.setClickable(true);
}
}
}
// And i also try to shrink image but it will not work for me
Bitmap ShrinkBitmap(String file, int width, int height) {
// bitmap factory
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
// bitmap decode
bmpFactoryOptions.inJustDecodeBounds = true;
//bitmap decode
Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight / (float) height);
int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth / (float) width);
if (heightRatio > 1 || widthRatio > 1) {
if (heightRatio > widthRatio) {
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
return bitmap;
}
}