I'm need to load a image from sharedPreferences to a pdf document.
The image loads normally when in normal use, but i don't know how to make it load in the pdf.
When I try to load it like a normal image I get "Unhandled Exception: type 'Image' is not a subtype of type 'PdfImage'"
This is how I use it normally.
import 'package:flutter/material.dart';
import 'package:flutter_settings_screens/flutter_settings_screens.dart';
import 'package:image_picker/image_picker.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:MyApp/SharedPrefUtility.dart';
Future<void> initSettings() async {
await Settings.init(
cacheProvider: SharePreferenceCache(),
);
}
class ProfilePage extends StatefulWidget {
#override
_ProfilePageState createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
Image logo;
pickImage(ImageSource source) async {
final _image = await ImagePicker.pickImage(source: ImageSource.gallery);
if (_image != null) {
setState(() {
logo = Image.file(_image);
});
ImageSharedPrefs.saveImageToPrefs(
ImageSharedPrefs.base64String(_image.readAsBytesSync()));
} else {
print('Error picking image!');
}
}
loadImageFromPrefs() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final imageKeyValue = prefs.getString(IMAGE_KEY);
if (imageKeyValue != null) {
final imageString = await ImageSharedPrefs.loadImageFromPrefs();
setState(() {
logo = ImageSharedPrefs.imageFrom64BaseString(imageString);
});
}
}
#override
void initState() {
super.initState();
loadImageFromPrefs();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
title: Text('Profile Settings'),
),
body: Center(
child: ListView(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
ClipRect(
child: Container(
width: 300,
height: 300,
child: logo == null ? Text('No image selected.') : logo,
),
),
RaisedButton(
onPressed: () {
pickImage(ImageSource.gallery);
},
child: Text('Pick Company Logo'),
),
],
),
],
),
),
);
}
}
With SharedPrefUtility.dart
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/widgets.dart';
import 'package:shared_preferences/shared_preferences.dart';
const IMAGE_KEY = 'IMAGE_KEY';
class ImageSharedPrefs {
static Future<bool> saveImageToPrefs(String value) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.setString(IMAGE_KEY, value);
}
static Future<String> loadImageFromPrefs() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString(IMAGE_KEY);
}
static String base64String(Uint8List data) {
return base64Encode(data);
}
static imageFrom64BaseString(String base64String) {
return Image.memory(
base64Decode(base64String),
fit: BoxFit.contain,
);
}
}
Any Suggestions would be great.
davey06 gave the answer on gitHub
final imageString = await ImageSharedPrefs.loadImageFromPrefs();
// Create a PDF document.
final document = pw.Document();
// Add page to the PDF
document.addPage(pw.Page(build: (context) {
return pw.Center(
child: pw.Image(
PdfImage.file(document.document, bytes: base64Decode(imageString)),
),
);
}));
// Return the PDF file content
return document.save();
https://github.com/DavBfr/dart_pdf/issues/477
"Unhandled Exception: type 'Image' is not a subtype of type 'PdfImage'" - it says you need to convert Image to PdfImage
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart ' as pw;
pdf = pw.Document();
PdfImage pdfImage = PdfImage.fromImage(pdf.document, image: logo);
pdf.addPage(
pw.Page(
pageFormat: PdfPageFormat.a4,
build: (context) {
return pw.Image(arcPdfImage, fit: pw.BoxFit.contain);
},
),
);
I'm using images taken from my assets to create PDF in this way:
PdfImage _logo = PdfImage.file(
doc.document,
bytes: (await rootBundle.load('assets/client-logo.png')).buffer.asUint8List(),
);
//later, during widget tree creation
pw.Image(_logo, width: 180);
It's not exactly what you're doing, but I think it's close enough. The PdfImage class can take as input any Uint8List for the bytes argument, so you should be able to use the same input you're using for the base64String method you defined for ImageSharedPrefs
Related
I'm trying to create a basic Hero animation between a gallery grid view page (using photo manager plugin) and a detail page. When the hero animation is done both back and forward, the picture is flickering .
Here is the example I've made :
The full code runnable :
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:photo_manager/photo_manager.dart';
void main() {
runApp(const TestApp());
}
class TestApp extends StatefulWidget {
const TestApp({Key? key}) : super(key: key);
#override
State<TestApp> createState() => _TestAppState();
}
class _TestAppState extends State<TestApp> {
final List<AssetEntity> assetsList = [];
bool granted = false;
void loadAssets() async {
granted = await PhotoManager.requestPermission();
if (granted) {
FilterOptionGroup option = FilterOptionGroup()
..addOrderOption(const OrderOption(
type: OrderOptionType.createDate,
asc: false,
));
final albums = await PhotoManager.getAssetPathList(filterOption: option, type: RequestType.image);
print("albums : $albums");
if (albums.isNotEmpty) {
var alb = albums.where((element) {
return element.name == 'Test';
});
var album = alb.first;
// Now that we got the album, fetch all the assets it contains
List<AssetEntity> currentList =
await album.getAssetListRange(start: 0, end: 200);
// Update the state and notify UI
assetsList.clear();
assetsList.addAll(currentList);
}
setState(() {});
}
}
#override
void initState() {
loadAssets();
super.initState();
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: granted
? _gridView()
: Center(
child: Container(
color: Colors.blue,
width: 200,
height: 200,
child: TextButton(
onPressed: () async {
granted = await PhotoManager.requestPermission();
setState(() {});
},
child: const Text(
"Ask permission",
style: TextStyle(color: Colors.white),
),
)),
)),
);
}
Widget _gridView() {
return GridView.builder(
itemCount: assetsList.length,
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
itemBuilder: (context, index) {
return Hero(
createRectTween: (Rect? begin, Rect? end) {
RectTween _rectTween = RectTween(begin: begin, end: end);
return _rectTween;
},
tag: assetsList[index].id,
child: GalleryThumbnail(
asset: assetsList[index],
onTap: (bytes) {
Navigator.push(context, MaterialPageRoute(builder: (context) {
return DetailsPage(asset: assetsList[index], bytes: bytes);
}));
},
));
});
}
}
class GalleryThumbnail extends StatelessWidget {
final AssetEntity asset;
final ValueChanged<Uint8List> onTap;
const GalleryThumbnail({Key? key, required this.asset, required this.onTap})
: super(key: key);
#override
Widget build(BuildContext context) {
return FutureBuilder<Uint8List?>(
future: Platform.isIOS
? asset.thumbDataWithOption(
ThumbOption.ios(
width: 500,
height: 500,
deliveryMode: DeliveryMode.opportunistic,
resizeMode: ResizeMode.fast,
resizeContentMode: ResizeContentMode.fit,
quality: 100
// resizeContentMode: ResizeContentMode.fill,
),
)
: asset.thumbDataWithSize(250, 250),
builder: (_, snapshot) {
final bytes = snapshot.data;
if (snapshot.hasError) {
return Container();
}
// If we have no data
if (bytes == null) return Container();
// If there's data, display it as an image
return GestureDetector(
onTap: () {
onTap(bytes);
},
child: Image.memory(bytes, fit: BoxFit.cover,gaplessPlayback: true,));
},
);
}
}
class DetailsPage extends StatefulWidget {
final AssetEntity asset;
final Uint8List bytes;
const DetailsPage({Key? key, required this.asset, required this.bytes})
: super(key: key);
#override
_DetailsPageState createState() => _DetailsPageState();
}
class _DetailsPageState extends State<DetailsPage> {
late ImageProvider _imageProvider;
Future<void> loadFile() async {
try {
File? file = await widget.asset.file;
if (file == null) return;
_imageProvider = Image.file(file).image;
setState(() {});
} catch (e) {
print("error to load file : " + e.toString());
}
}
#override
void initState() {
_imageProvider = Image.memory(widget.bytes).image;
loadFile();
super.initState();
}
#override
Widget build(BuildContext context) {
double ratio = widget.asset.height / widget.asset.width;
return Scaffold(
body: Stack(
children: [
Hero(
createRectTween: (Rect? begin, Rect? end) {
RectTween _rectTween = RectTween(begin: begin, end: end);
return _rectTween;
},
tag: widget.asset.id,
child: Center(
child: Image(image: _imageProvider,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height*ratio,
gaplessPlayback: true),
),
),
const SafeArea(
child: Padding(
padding: EdgeInsets.all(8.0),
child: BackButton(),
)),
],
),
);
}
}
I've implemented hero animation somewhere else in my project and I'm not getting this behavior where as here I do.
Why this is happening and how to correct this ?
Add
transitionOnUserGestures: true,
to Hero() widget in current & next page
This happens because the Hero of the image thumbail wraps the FutureBuilder, and then the future triggers again during the animation, wraps the Image instead of the FutureBuilder.
I want to Pick a image and then display the picked Image. So I tried doing this->
class ImageUploadChoice extends StatefulWidget {
#override
_ImageUploadChoiceState createState() => _ImageUploadChoiceState();
}
class _ImageUploadChoiceState extends State<ImageUploadChoice> {
File imageFile;
Future getImage(int type) async {
PickedFile pickedImage = await ImagePicker().getImage(
source: type == 1 ? ImageSource.camera : ImageSource.gallery,
imageQuality: 50
);
return pickedImage;
}
For Displaying Image
Expanded(
child: Container(
child: imageFile != null
? Image.file(
imageFile,
height: MediaQuery.of(context).size.height / 5,
)
: Text("Pick up the image"),
),
),
For Calling Function->
new ListTile(
leading: new Icon(
Icons.photo_library,
color: Colors.black,),
title: new Text(
'Photo Library',
style: getTextStyle(MediaQuery.of(context).size.height, "heading2"),
),
onTap: () async {
final tmpFile = await getImage(2);
setState(() {
imageFile = tmpFile;
});
Navigator.pop(context);
Navigator.of(context).pop();
}),
But It's not Working. The Selected Image is not been displayed. What else I need to do?
Made a sample code and it's working fine. Remove following lines
Navigator.pop(context);
Navigator.of(context).pop();
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Test(),
);
}
}
class Test extends StatefulWidget {
#override
_Test createState() => _Test();
}
class _Test extends State<Test> {
File imageFile;
Future getImage() async {
final pickedFile = await ImagePicker().getImage(source: ImageSource.gallery);
setState(() {
if (pickedFile != null) {
imageFile = File(pickedFile.path);
} else {
print('No image selected.');
}
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: InkWell(
onTap: (){
getImage();
},
child: Icon(
Icons.add
),
),
body: Center(
child: root(),
),
);
}
Widget root() {
return Container(
child: imageFile != null
? Image.file(
imageFile,
height: MediaQuery
.of(context)
.size
.height / 5,
)
: Text("Pick up the image"),
);
}
}
I'm trying to load multiple images (from assets or cache) into the pdf, but it does not work.
There are no errors, the pdf is created, but is corrupted somehow and does not want to open.
Everything is 100% if there is only 1 image, or if all the images are the same image.
What am I doing wrong?
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(backgroundColor: Colors.red, title: Text('Images')),
body: Center(child: HomeScreen())));
}
}
class HomeScreen extends StatefulWidget {
HomeScreenState createState() => HomeScreenState();
}
class HomeScreenState extends State<HomeScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ListView(
children: <Widget>[
RaisedButton(
color: Colors.red,
onPressed: () async {
writeOnPdf();
await savePdf();
},
child: Text('Create Pdf'),
),
],
),
));
}
}
final pdf = pw.Document();
writeOnPdf() async {
var assetImage = PdfImage.file(
pdf.document,
bytes: (await rootBundle.load('assets/aa.png')).buffer.asUint8List(),
);
var otherImage = PdfImage.file(
pdf.document,
bytes: (await rootBundle.load('assets/out.png')).buffer.asUint8List(),
);
pdf.addPage(pw.MultiPage(
pageFormat: PdfPageFormat.a4,
margin: pw.EdgeInsets.all(32),
build: (pw.Context context) {
return <pw.Widget>[
pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.center,
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
children: <pw.Widget>[
pw.ClipRect(
child: pw.Container(
width: 550,
height: 250,
child: pw.Image(assetImage),
),
),
]),
pw.ClipRect(
child: pw.Container(
width: 100,
height: 100,
child: pw.Image(otherImage),
),
)
];
}));
}
final bool isHTML = false;
Future savePdf() async {
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
File file = File('$tempPath/1234.pdf');
file.writeAsBytesSync(pdf.save());
print(tempDir.listSync());
}
It would be ideal if I could load one image from the assets and one image from the cache. But at the moment nothing works.
Any advice would be appreciated.
DavBfr gave the answer. "It's an issue with async/await."
Multiple Images #425]1
RaisedButton(
color: Colors.red,
onPressed: () async {
final pdf = await writeOnPdf();
await savePdf(pdf);
},
child: const Text('Create Pdf'),
),
try like that. its not influent way but it works for multiple images in same pdf page.
final ByteData bytes1 = await rootBundle.load('assets/images/logo.png');
final Uint8List img1 = bytes1.buffer.asUint8List();
final ByteData bytes2 = await rootBundle.load('assets/images/1.jpg');
final Uint8List img2 = bytes2.buffer.asUint8List();
final ByteData bytes3 = await rootBundle.load('assets/images/2.png');
final Uint8List img3 = bytes3.buffer.asUint8List();
pw.Image imgty(Uint8List img) {
return pw.Image(
pw.MemoryImage(
img,
),
fit: pw.BoxFit.contain);
}
and use them in pdf page
Container(
height: 111,
width: 111,
child: imgty(img1),)
I take a picture with phone's camera, then resize it and convert it to base64 string. However, after those manipulations, base64 string that I get doesn't seem to be valid.
I try to convert it back to image on this website https://codebeautify.org/base64-to-image-converter . After I click generate image, nothing happens, however sample on their website works. Tried on the other websites and still no luck.
My code:
import 'package:image/image.dart' as img;
import 'package:image_picker/image_picker.dart';
File _photo;
String photoBase64;
Future getImage(ImageSource source) async {
var photo = await ImagePicker.pickImage(source: source);
img.Image image = img.decodeImage(photo.readAsBytesSync());
img.Image imageResized = img.copyResize(image, width: 120);
setState(() {
_photo = photo;
List<int> imageBytes = imageResized.getBytes();
photoBase64 = base64Encode(imageBytes);
});
}
I tried base64UrlEncode() too, however the issue still remains. String I am trying to convert back to image is photoBase64. My goal is to send it in a body of a POST request later. What exactly am I doing wrong here?
Thank you
You can copy paste run full code below
You can use package https://pub.dev/packages/flutter_native_image to get resized image
imageResized = await FlutterNativeImage.compressImage(photo.path,
quality: 100, targetWidth: 120, targetHeight: 120);
...
List<int> imageBytes = imageResized.readAsBytesSync();
working demo
full code
import 'package:flutter/material.dart';
import 'package:image/image.dart' as img;
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'dart:convert';
import 'package:flutter_native_image/flutter_native_image.dart';
void main() => runApp(MyApp());
File _photo;
String photoBase64;
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
File imageResized;
Future getImage(ImageSource source) async {
var photo = await ImagePicker.pickImage(source: source);
imageResized = await FlutterNativeImage.compressImage(photo.path,
quality: 100, targetWidth: 120, targetHeight: 120);
setState(() {
_photo = photo;
List<int> imageBytes = imageResized.readAsBytesSync();
photoBase64 = base64Encode(imageBytes);
print(photoBase64);
});
}
void _incrementCounter() {
getImage(ImageSource.camera);
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
imageResized == null ? Container() : Image.file(imageResized),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
my solution
Future getImage() async {
pickedFile =
await _picker.getImage(source: ImageSource.gallery, imageQuality: 50);
setState(() {
if (pickedFile != null) {
file = File(pickedFile!.path);
final bytes =
file!.readAsBytesSync();
img64 = base64Encode(bytes);
} else {
print('No image selected.');
}
});
}
I am trying to display all the phone gallery images myself by reading the external files directory and possibly every image that ends with jpg or png. I achieved that, but could not display all of them in a grid as due to their sizes or the no. of images, the app crashes. Code looks bit like this..
new GridView.count(
shrinkWrap: true,
physics: new ClampingScrollPhysics(),
crossAxisCount: 2,
// children: new List<Widget>.generate(_images.length, (index) {
// children: new List<Widget>.generate(allImages.length, (index) {
children: new List<Widget>.generate(_PhoneImages.length, (index) {
File imgFile = _phoneImageFiles[index];
thumbBytes = _phoneThumbBytes[index]; // assuming it got created!!!
// print('thumbbytes $thumbBytes');
print('phone image index: $index');
return new GridTile(
child: new GestureDetector(
child: new Stack(
children: [
new Card(
// color: Colors.blue.shade200,
color: Colors.white70,
child: new Center(
// child: new Text('tile $index'),
// child: new Image.asset(_images[index]),
/*
child: new CachedNetworkImage(
imageUrl: allImages[index].path,
// placeholder: new CircularProgressIndicator(),
errorWidget: new Icon(Icons.error),
)
*/
child: new Image.file(imgFile,
// child: new Image.memory(thumbBytes,
So I tried the imageresize library which tells me to do a heavy operation of resizing, that takes almost 20 minutes before I can show the thumbnails.
All I need is to read thumbnails from gallery like how the phone gallery displays. I don't need categorization. I need all and a link to their full version so that I can do something with them later on.
I think this might help multi_image_picker
e.g
import 'package:flutter/material.dart';
import 'package:multi_image_picker/asset.dart';
class AssetView extends StatefulWidget {
final int _index;
final Asset _asset;
AssetView(this._index, this._asset);
#override
State<StatefulWidget> createState() => AssetState(this._index, this._asset);
}
class AssetState extends State<AssetView> {
int _index = 0;
Asset _asset;
AssetState(this._index, this._asset);
#override
void initState() {
super.initState();
_loadImage();
}
void _loadImage() async {
await this._asset.requestThumbnail(300, 300); // here requesting thumbnail
setState(() {});
}
#override
Widget build(BuildContext context) {
if (null != this._asset.thumbData) {
return Image.memory(
this._asset.thumbData.buffer.asUint8List(),
fit: BoxFit.cover,
);
}
return Text(
'${this._index}',
style: Theme.of(context).textTheme.headline,
);
}
}
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:multi_image_picker/asset.dart';
import 'package:multi_image_picker/multi_image_picker.dart';
import 'asset_view.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<Asset> images = List<Asset>();
String _error;
#override
void initState() {
super.initState();
}
Widget buildGridView() {
return GridView.count(
crossAxisCount: 3,
children: List.generate(images.length, (index) {
return AssetView(index, images[index]);
}),
);
}
Future<void> loadAssets() async {
setState(() {
images = List<Asset>();
});
List resultList;
String error;
try {
resultList = await MultiImagePicker.pickImages(
maxImages: 300,
);
} on PlatformException catch (e) {
error = e.message;
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
images = resultList;
if (error == null) _error = 'No Error Dectected';
});
}
#override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Plugin example app'),
),
body: Column(
children: <Widget>[
Center(child: Text('Error: $_error')),
RaisedButton(
child: Text("Pick images"),
onPressed: loadAssets,
),
Expanded(
child: buildGridView(),
)
],
),
),
);
}
}