import 'dart:convert'; import 'dart:io'; import 'package:get/get.dart'; import 'package:image_cropper/image_cropper.dart'; import 'package:image_picker/image_picker.dart'; import 'package:http/http.dart' as http; import 'package:image/image.dart' as img; import 'package:path_provider/path_provider.dart'; import '../../constant/api_key.dart'; import '../../constant/colors.dart'; class ContentController extends GetxController { final picker = ImagePicker(); bool isloading = false; var image; CroppedFile? croppedFile; Future pickImage() async { final pickedImage = await picker.pickImage(source: ImageSource.gallery); if (pickedImage != null) { image = File(pickedImage.path); // Crop the image croppedFile = await ImageCropper().cropImage( sourcePath: image!.path, aspectRatioPresets: [ CropAspectRatioPreset.square, CropAspectRatioPreset.ratio3x2, CropAspectRatioPreset.original, CropAspectRatioPreset.ratio4x3, CropAspectRatioPreset.ratio16x9 ], uiSettings: [ AndroidUiSettings( toolbarTitle: 'Cropper'.tr, toolbarColor: AppColor.blueColor, toolbarWidgetColor: AppColor.yellowColor, initAspectRatio: CropAspectRatioPreset.original, lockAspectRatio: false), IOSUiSettings( title: 'Cropper'.tr, ), ], ); // image = croppedFile; // Resize the image final rawImage = img.decodeImage(File(croppedFile!.path).readAsBytesSync()); final resizedImage = img.copyResize(rawImage!, width: 800); // Adjust the width as needed final appDir = await getTemporaryDirectory(); final resizedImagePath = '${appDir.path}/resized_image.jpg'; final resizedImageFile = File(resizedImagePath); resizedImageFile.writeAsBytesSync( img.encodeJpg(resizedImage)); // Save the resized image as JPEG image = resizedImageFile; update(); } } Future generateContent() async { await pickImage(); if (image != null) { final imageBytes = await image.readAsBytes(); final imageData = base64Encode(imageBytes); var requestBody = jsonEncode({ 'contents': [ { 'parts': [ { 'inlineData': { 'mimeType': 'image/jpeg', 'data': imageData, }, }, { 'text': 'write json for all data as first name ,last name,dob,licenseID,expiration date,issued date asdress class type ,output json type', }, ], }, ], 'generationConfig': { 'temperature': 0.4, 'topK': 32, 'topP': 1, 'maxOutputTokens': 4096, 'stopSequences': [], }, 'safetySettings': [ { 'category': 'HARM_CATEGORY_HARASSMENT', 'threshold': 'BLOCK_MEDIUM_AND_ABOVE', }, { 'category': 'HARM_CATEGORY_HATE_SPEECH', 'threshold': 'BLOCK_MEDIUM_AND_ABOVE', }, { 'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'threshold': 'BLOCK_MEDIUM_AND_ABOVE', }, { 'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'threshold': 'BLOCK_MEDIUM_AND_ABOVE', }, ], }); print(requestBody); final response = await http.post( Uri.parse( 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro-vision:generateContent?key=${AK.geminiApi}'), headers: {'Content-Type': 'application/json'}, body: requestBody, ); if (response.statusCode == 200) { var responseData = jsonDecode(response.body); // Process the responseData as needed // print(responseData); var result = responseData['candidates'][0]['content']['parts'][0]['text']; // print(jsonEncode(result)); // print((result)); // print(result['dob']); RegExp regex = RegExp(r"```json([^`]*)```"); String? jsonString = regex.firstMatch(responseData.toString())?.group(1)?.trim(); if (jsonString != null) { // Convert the JSON object to a String jsonString = jsonEncode(json.decode(jsonString)); print(jsonString); } else { print("JSON string not found"); } // Rest of your code... } else { print('Request failed with status: ${response.statusCode}'); print('Request failed with status: ${response.body}'); } } else { print('No image selected'); } } Future getFromCarRegistration() async { await pickImage(); if (image != null) { final imageBytes = await image.readAsBytes(); final imageData = base64Encode(imageBytes); var requestBody = jsonEncode({ 'contents': [ { 'parts': [ { 'inlineData': { 'mimeType': 'image/jpeg', 'data': imageData, }, }, { 'text': 'write output json from image for[ vin, make, model, year, expiration_date, color, owner, registration_date ],output json type ', }, ], }, ], 'generationConfig': { 'temperature': 0.4, 'topK': 32, 'topP': 1, 'maxOutputTokens': 4096, 'stopSequences': [], }, 'safetySettings': [ { 'category': 'HARM_CATEGORY_HARASSMENT', 'threshold': 'BLOCK_MEDIUM_AND_ABOVE', }, { 'category': 'HARM_CATEGORY_HATE_SPEECH', 'threshold': 'BLOCK_MEDIUM_AND_ABOVE', }, { 'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'threshold': 'BLOCK_MEDIUM_AND_ABOVE', }, { 'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'threshold': 'BLOCK_MEDIUM_AND_ABOVE', }, ], }); print(requestBody); final response = await http.post( Uri.parse( 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro-vision:generateContent?key=${AK.geminiApi}'), headers: {'Content-Type': 'application/json'}, body: requestBody, ); if (response.statusCode == 200) { var responseData = jsonDecode(response.body); // Process the responseData as needed var result = responseData['candidates'][0]['content']['parts'][0]['text']; print(result); RegExp regex = RegExp(r"```json([^`]*)```"); String? jsonString = regex.firstMatch(responseData.toString())?.group(1)?.trim(); if (jsonString != null) { // Convert the JSON object to a String jsonString = jsonEncode(json.decode(jsonString)); print(jsonString); } else { print("JSON string not found"); } // Rest of your code... } else { print('Request failed with status: ${response.statusCode}'); print('Request failed with status: ${response.body}'); } } else { print('No image selected'); } } @override void onInit() { // generateContent(); super.onInit(); } }