I want to plot a diagram in android with GraphView library http://www.android-graphview.org/ . but the number of points of diagram is more than 12000 and it isn't possible to add all single points manually. my data saved in storage (with custom format) as shown below :
0.000,116.288
0.008,122.422
0.016,126.721
...
I get the data from https://physionet.org/cgi-bin/atm/ATM
with this setting:
Signals:ABP
Time format:seconds
Toolbox:Export signals as CSV
and I need to read them from file and convert data as shown below for plotting diagram :
new DataPoint(0.000,116.288),
new DataPoint(0.008,122.422),
new DataPoint(0.016,126.721)...
I copied my CSV file in asset folder and read it. then I convert them into Double and try to plot diagram with data.
but the diagram is not correct . I think the problems appears when I want to add new Datapoint, because it need to add a comma "," after each line
pls advise how I can add it?
besides,sometimes after running the application it has stopped.
java code:
public class Plot_Activity extends AppCompatActivity {
String valXY[];
Double Xval;
Double Yval;
GraphView graph;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GraphView graph = (GraphView) findViewById(R.id.graph);
try {
reader=new BufferedReader(new InputStreamReader(getAssets().open("plot/b00_2010_abp_5s.txt")));
String mline;
while((mline=reader.readLine())!=null)
{
valXY = mline.split(",");
Xval =Double.parseDouble(valXY[0]);
Yval =Double.parseDouble(valXY[1]);
DataPoint[] dp = new DataPoint[valXY.length];
for (int i = 0; i < valXY.length; i++)
{
dp[i] = new DataPoint(Xval, Yval);
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dp);
graph.addSeries(series);
}
} catch (IOException e)
{
e.printStackTrace();
}
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setMinX(0);
graph.getViewport().setMaxX(1);
graph.getViewport().setScrollable(true); // enables horizontal scrolling
graph.getViewport().setScrollableY(true); // enables vertical scrolling
}
}
XML code:
<com.jjoe64.graphview.GraphView
android:id="#+id/graph"
android:layout_marginRight="11dp"
android:layout_width="wrap_content"
android:layout_height="280dp"
thank in advance
java code:
with using Graphview library:
public class Plot_Activity extends AppCompatActivity {
String valXY[];
Double Xval;
Double Yval;
GraphView graph;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GraphView graph = (GraphView) findViewById(R.id.graph);
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(getAssets().open("plot/excel_data_abp.csv")));
reader.readLine(); //skip first line of file
reader.readLine(); //skip second line of file
String mline;
ArrayList<DataPoint> arrDataPoint=new ArrayList<>();
while ((mline = reader.readLine()) != null) {
valXY = mline.split(",");
Xval = Double.parseDouble(valXY[0]);
Yval = Double.parseDouble(valXY[1]);
DataPoint dp = new DataPoint(Xval, Yval);
arrDataPoint.add(dp);
}
DataPoint[] listDp = new DataPoint[arrDataPoint.size()];
for(int i=0;i<arrDataPoint.size();i++){
listDp[i]=arrDataPoint.get(i);
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(listDp);
graph.addSeries(series);
} catch (IOException e) {
e.printStackTrace();
}
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setMinX(0);
graph.getViewport().setMaxX(1);
graph.getViewport().setScrollable(true); // enables horizontal scrolling
graph.getViewport().setScrollableY(true); // enables vertical scrolling
}
}
You have to tackle this one step by step.
First you want to read a CSV file. Search for "parse csv file java" and you'll find many tutorials on how to do this.
As you parse through the csv file you'll want to build an array (or two) from the values collected.
Use these values in a for loop to generate new data points. So instead of manually entering each value, you'll have something that looks more like this:
DataPoint[] dp = new DataPoint[yourCSVArray.size()];
for (int i = 0; i < yourCSVArray.size(); i++) {
dp[i] = new DataPoint(variableX, variableY);
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dp);
graph.addSeries(series);
Now that you have some direction, try piecing some code together and if you run into trouble post your code for help.
Related
I have Tiff file of population density in the world from Sadac.
I try to read this file in java and extract density number for specific point (LatLon).
I tried opening and reading the file with Geotools, but i dont know, how to extract specific point and its fields (density). I tried many ways but none of them worked.
My code actualy:
File f = new File("/opt/gpw-v4-population-density_2020.tif");
AbstractGridFormat format = GridFormatFinder.findFormat(f);
AbstractGridCoverage2DReader reader = format.getReader(f);
CoordinateReferenceSystem crs = reader.getCoordinateReferenceSystem();
System.out.println(crs);
GridCoverage2D cov = null;
try {
cov = reader.read(null);
} catch (Exception e) {
//todo
}
Can anyone advise me how to get to the point and its fields?
Thank you for advice.
Resolved.
private static GridCoverage2D grid;
private static Raster gridData;
private static void initTif() throws Exception {
File tiffFile = new File("/opt/gpw-v4-population-density_2020.tif");
GeoTiffReader reader = new GeoTiffReader(tiffFile);
grid = reader.read(null);
RenderedImage image = grid.getRenderedImage();
if (image != null) {
gridData = image.getData();
}
}
public void getDensity(double x, double y) throws InvalidGridGeometryException, TransformException {
GridGeometry2D gg = grid.getGridGeometry();
DirectPosition2D posWorld = new DirectPosition2D(x, y);
GridCoordinates2D posGrid = gg.worldToGrid(posWorld);
double[] pixel = new double[1];
double[] data = gridData.getPixel(posGrid.x, posGrid.y, pixel);
for (double d : data) {
System.out.println(d);
}
}
Following this tutorial from openCV, and it should be straight forward. However, it crashes with an assertion fail on the net.forward, that I cannot resolve/find anywhere else.
Thought this problem seemed similar and tried to go through the fix/problem finding. However, restarting the discussion and trials showed it is likely not the same. I used initially 3.4.3, which did not support the same Mat type somehow. Updated to 3.4.7 now, and can confirm the blob size is okay (generated from image). Tried also various other prototxt and caffemodels, but doubt by now that the problem lies there (works if the files are okay, otherwise the net loading fails). The key code should be this:
// Load a network.
public void onCameraViewStarted(int width, int height) {
String proto = getPath("deploy.prototxt", this);
String weights = getPath("MobileNetSSD_deploy.caffemodel", this);
net = Dnn.readNetFromCaffe(proto, weights);
Log.i(TAG, "Network loaded successfully");
}
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
// Get a new frame
Mat frame = inputFrame.rgba();
Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2RGB);
// Forward image through network.
Mat blob = Dnn.blobFromImage(frame, 0.007843,
new Size(300, 300),
new Scalar(127.5, 127.5, 127.5));
net.setInput(blob);
Mat detections = net.forward(); //***215 ASSERTION FAILED occurs***
int cols = frame.cols();
int rows = frame.rows();
detections = detections.reshape(1, (int)detections.total() / 7);
for (int i = 0; i < detections.rows(); ++i) {
double confidence = detections.get(i, 2)[0];
if (confidence > 0.2) {
int classId = (int)detections.get(i, 1)[0];
int left = (int)(detections.get(i, 3)[0] * cols);
int top = (int)(detections.get(i, 4)[0] * rows);
int right = (int)(detections.get(i, 5)[0] * cols);
int bottom = (int)(detections.get(i, 6)[0] * rows);
// Draw rectangle around detected object.
Imgproc.rectangle(frame, new Point(left, top), new Point(right, bottom),
new Scalar(0, 255, 0));
String label = classNames[classId] + ": " + confidence;
int[] baseLine = new int[1];
Size labelSize = Imgproc.getTextSize(label, Core.FONT_HERSHEY_SIMPLEX, 0.5, 1, baseLine);
// Draw background for label.
Imgproc.rectangle(frame, new Point(left, top - labelSize.height),
new Point(left + labelSize.width, top + baseLine[0]),
new Scalar(255, 255, 255), Core.FILLED);
// Write class name and confidence.
Imgproc.putText(frame, label, new Point(left, top),
Core.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar(0, 0, 0));
}
}
return frame;
}
public void onCameraViewStopped() {}
// Upload file to storage and return a path.
private static String getPath(String file, Context context) {
AssetManager assetManager = context.getAssets();
BufferedInputStream inputStream = null;
try {
// Read data from assets.
inputStream = new BufferedInputStream(assetManager.open(file));
byte[] data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
// Create copy file in storage.
File outFile = new File(context.getFilesDir(), file);
FileOutputStream os = new FileOutputStream(outFile);
os.write(data);
os.close();
// Return a path to file which may be read in common way.
return outFile.getAbsolutePath();
} catch (IOException ex) {
Log.i(TAG, "Failed to upload a file");
}
return "";
}
The full error message is
cv::Exception: OpenCV(3.4.7) /build/3_4_pack-android/opencv/modules/dnn/src/layers/batch_norm_layer.cpp:39: error: (-215:Assertion failed) blobs.size() >= 2 in function 'cv::dnn::BatchNormLayerImpl::BatchNormLayerImpl(const cv::dnn::experimental_dnn_34_v13::LayerParams&)'
I expect it to not crash. The frame should be okay (image loaded), the net is not empty, and the layers in the net seem fine too (checked since there are some differences using caffe in java). Any help is appreciated!
After some days of research in different directions, I found the problem: the frame format should be BGR, not RGB! That means
Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2BGR);
I have a raw fax file (G3/T.4 format) and need to convert it into a multi-page TIFF programmatically via Java. JAI has not been successful for me so far even if I think it should work. Tools from sFaxTools have been successful for converting my raw fax files into TIFF (Batch Fax2Tif or Faxsee), but I need to do this programmatically via Java. I think there should be a possibility using java advanced imaging, please check the code snipplet below:
private void writeTiff(byte[] buffer, OutputStream outStream) {
try {
//reading image from given buffer
RenderedImage rendImage = null;
TIFFDecodeParam decodeParams = new TIFFDecodeParam();
ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", stream, decodeParams);
TIFFEncodeParam encodeParams = new TIFFEncodeParam();
int numPages = decoder.getNumPages();
for (int i = 0; i < numPages; i++) {
rendImage = decoder.decodeAsRenderedImage(i);
ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF", outStream, encodeParams);
encoder.encode(rendImage);
}
} catch (Exception e) {
e.printStackTrace();
} catch (Error err) {
err.printStackTrace();
}
}
The problem is, that the reading section especially
ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", stream, decodeParams);
should be replaced by some ImageDecoder implementation which internally uses a FaxDecoder for decoding a g3 raw fax file. There is a protected class TIFFFaxDecoder within the the jai package, is it possible and how to use this for my purpose? Any idea?
Thanks
I don't think JAI supports reading G3/T.4 raw fax data directly. However, here's sample code you can modify and extend to suit your needs, implementing the idea outlined in the comments (originally posted as a Gist).
It does not decode the G3/T.4 data in any way, it simply wraps the raw fax data in a minimal TIFF container. This allows the data to be read as a normal TIFF later. It uses (my own) TwelveMonkeys ImageIO library to do so.
If you don't know the width/height of the fax files, you might be able to implement an algorithm to find them, by using the CCITTFaxDecoderStream, trying the different widths (columns) defined in the standard, and see how many whole lines you can read. If you got the numbers right, you should fully consume the stream.
import com.twelvemonkeys.imageio.metadata.AbstractEntry;
import com.twelvemonkeys.imageio.metadata.Entry;
import com.twelvemonkeys.imageio.metadata.exif.EXIFWriter;
import com.twelvemonkeys.imageio.metadata.exif.Rational;
import com.twelvemonkeys.imageio.metadata.exif.TIFF;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.io.*;
import java.util.ArrayList;
public class G3Test {
public static void main(String[] args) throws IOException {
File input = new File(args[0]);
File output = new File(args.length > 1 ? args[1] : input.getName().replace(".g3", ".tif"));
// ImageWidth = 1728, 2048, 2482. SHORT or LONG. These are the fixed page widths in pixels defined in CCITT Group 3.
int columns = 1728; // The default
int rows = 100; // Trial and error for sample file found at http://www.filesuffix.com/en/extension/fax
ArrayList<Entry> entries = new ArrayList<>();
// http://cool.conservation-us.org/bytopic/imaging/std/tiff-f.html
// Required Class F tags
entries.add(new TIFFEntry(TIFF.TAG_COMPRESSION, TIFF.TYPE_SHORT, 3)); // CCITT T.4
entries.add(new TIFFEntry(TIFF.TAG_FILL_ORDER, TIFF.TYPE_SHORT, 1)); // Left to right
entries.add(new TIFFEntry(TIFF.TAG_GROUP3OPTIONS, TIFF.TYPE_LONG, 0)); // No options set
entries.add(new TIFFEntry(TIFF.TAG_IMAGE_WIDTH, TIFF.TYPE_LONG, columns));
entries.add(new TIFFEntry(TIFF.TAG_IMAGE_HEIGHT, TIFF.TYPE_LONG, rows));
entries.add(new TIFFEntry(TIFF.TAG_SUBFILE_TYPE, TIFF.TYPE_LONG, 2)); // Page
entries.add(new TIFFEntry(TIFF.TAG_RESOLUTION_UNIT, TIFF.TYPE_SHORT, 2)); // Inches
entries.add(new TIFFEntry(TIFF.TAG_X_RESOLUTION, TIFF.TYPE_RATIONAL, new Rational(204))); // 204
entries.add(new TIFFEntry(TIFF.TAG_Y_RESOLUTION, TIFF.TYPE_RATIONAL, new Rational(98))); // 98, 196
// Required Bilevel (Class B) tags
entries.add(new TIFFEntry(TIFF.TAG_BITS_PER_SAMPLE, TIFF.TYPE_SHORT, 1)); // 1 bit/sample
entries.add(new TIFFEntry(TIFF.TAG_PHOTOMETRIC_INTERPRETATION, TIFF.TYPE_SHORT, 0)); // White is zero
entries.add(new TIFFEntry(TIFF.TAG_SOFTWARE, TIFF.TYPE_ASCII, "TwelveMonkeys FAX2TIFF 0.1 BETA ;-)"));
entries.add(new TIFFEntry(TIFF.TAG_ROWS_PER_STRIP, TIFF.TYPE_LONG, rows));
entries.add(new TIFFEntry(TIFF.TAG_SAMPLES_PER_PIXEL, TIFF.TYPE_SHORT, 1)); // 1 sample/pixel
entries.add(new TIFFEntry(TIFF.TAG_STRIP_BYTE_COUNTS, TIFF.TYPE_LONG, input.length()));
entries.add(new TIFFEntry(TIFF.TAG_STRIP_OFFSETS, TIFF.TYPE_LONG, -1)); // placeholder for now
// We now have all our entries, compute size of the entries, and make that the offset (we'll write the data right after).
EXIFWriter writer = new EXIFWriter();
long offset = 12 + writer.computeIFDSize(entries); // + 12 for TIFF magic (4), IFD0 pointer (4) and EOF (4)
entries.remove(entries.size() - 1);
entries.add(new TIFFEntry(TIFF.TAG_STRIP_OFFSETS, TIFF.TYPE_LONG, offset));
try (InputStream in = new FileInputStream(input)) {
try (ImageOutputStream out = ImageIO.createImageOutputStream(output)) {
// Write the TIFF IFD for the image data
writer.write(entries, out);
// Copy the already G3 compressed bytes verbatim to the output
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) >= 0) {
out.write(buffer, 0, read);
}
}
}
}
// API stupidity, should be fixed in later verisons (ie. contain a predefined TIFFEntry class)
static final class TIFFEntry extends AbstractEntry {
private final short type;
TIFFEntry(int identifier, short type, Object value) {
super(identifier, value);
this.type = type;
}
#Override
public String getTypeName() {
return TIFF.TYPE_NAMES[type];
}
}
}
For not guessing the image height, its possible to find out the number of rows.
If you know how the image look like, you can read the encoded image data bitwise (take note of the bitorder) and count the 'EOL flags'. There are two different flags, dependent of the row begins with a white pixel or a black. The full description is here Tiff Format Specification under section : Modified Huffman Compression.
I have a specific problem which has not be answered yet on stackoverflow; I have images in the assets folder numbered like 0.jpg, 1.jpg, 2.jpg etc. Using a for loop I select three images from the asssets folder and I am trying to add these images to a gridview but the images are not showing. The activity starts up okay just no images!
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
gridView = (GridView) findViewById(R.id.gridview_result);
// Sets the Tag
gridView.setTag(GRIDVIEW_TAG);
/*
* Adapt the image for the GridView format
*/
imageAdapter = new ImageGridViewAdapter(getApplicationContext());
gridView.setAdapter(imageAdapter);
// Set the orientation to landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// Retrieve 3 images form the database which appear
// similar
for (int i = 0; i < 3; i++) {
// System.out.println(Retrieval.distances[i][0]);
image = Retrieval.distances[i][0];
int num = (int) image;
StringBuilder sBuilder = new StringBuilder();
sBuilder.append(num);
String imageNum = sBuilder.toString();
System.out.println(imageNum);
String file = imageNum + ".jpg";
try {
// get input stream
InputStream ims = getAssets().open(file);
Log.i("ERROR_IMS", ims + "");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, file);
// set image to ImageView
gridView.setBackground(d);
Log.i("ERROR_d", d + "");
Log.i("ERROR_gridview", gridView+"");
} catch (IOException ex) {
Log.e("I/O ERROR", "Failed when ...");
}
}
}
I believe the issue is occurring in the try/catch. Any help will be much appreciated!
You should get all images first and set it to your adapter.
// set image to ImageView
gridView.setBackground(d);
doesn't affect to your grid items view.
A good tutorial for it: guide
I want to convert my excel file as well as its entites(charts, tables, images) to jpeg/png images. Currently using aspose for that. Here is my code
public static int excelToImages(final String sourceFilePath, final String outFilePrefix) throws Exception {
int noOfImages = 0;
Workbook workbook = getWorkbook(sourceFilePath);
List<Worksheet> worksheets = getAllWorksheets(workbook);
if (worksheets != null) {
for (Worksheet worksheet : worksheets) {
if (worksheet.getCells().getCount() > 0) {
String outFilePath = FileUtils.getAbsoluteFilePath(outFilePrefix + (noOfImages++));
SheetRender sr = new SheetRender(worksheet, getImageOrPrintOptions());
sr.toImage(0, outFilePath);
}
}
}
return noOfImages;
}
private static ImageOrPrintOptions getImageOrPrintOptions() {
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.setImageFormat(ImageFormat.getJpeg());
imgOptions.setOnePagePerSheet(true);
return imgOptions;
}
private static List<Worksheet> getAllWorksheets(final Workbook workbook) {
List<Worksheet> worksheets = new ArrayList<Worksheet>();
WorksheetCollection worksheetCollection = workbook.getWorksheets();
for (int i = 0; i < worksheetCollection.getCount(); i++) {
worksheets.add(worksheetCollection.get(i));
}
return worksheets;
}
My problem is that size of output image is either split into multiple A4 size or single 1 sheet depends upon the value of
imgOptions.setOnePagePerSheet(true);
Can anybody tell me how I can customize the size of output image file?
You can try it with imgOptions.setOnlyArea(true);. That will set the size of the image to the minimal that's needed to put everything to the image. But I'm not sure if the generated image is split into A4 parts.