Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
    
 * Copyright (c)  2010-2013 Samsung Electronics Co., Ltd. All rights reserved.
    
 *
    
 * Mobile Communication Division,
    
 * Digital Media & Communications Business, Samsung Electronics Co., Ltd.
    
 *
    
 * This software and its documentation are confidential and proprietary
    
 * information of Samsung Electronics Co., Ltd.  No part of the software and
    
 * documents may be copied, reproduced, transmitted, translated, or reduced to
    
 * any electronic medium or machine-readable form without the prior written
    
 * consent of Samsung Electronics.
    
 *
    
 * Samsung Electronics makes no representations with respect to the contents,
    
 * and assumes no responsibility for any errors that might appear in the
    
 * software and documents. This publication and the contents hereof are subject
    
 * to change without notice.
    
 *
    
    
    
 * ========================================================================= 
    
 * Alternatively, the contents of this file may be used under the terms of
    
 * either the GNU General Public License Version 2.
    
 * 
    
 *
    
 * Copyright (c)  2010-2013 Samsung Electronics Co., Ltd. All rights reserved.
    
 *
    
 * This software is licensed under the terms of the GNU General Public
    
 * License version 2, as published by the Free Software Foundation, and
    
 * may be copied, distributed, and modified under those terms.
    
 *
    
 * This program is distributed in the hope that it will be useful,
    
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    
 * GNU General Public License for more details.
    
 */

import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.media.ExifInterface;
import android.util.Log;

public class Main {
    public static final String TAG = "GenericPrintService";
    public static final String unknown = "unknown";
    public static final String portrait = "portrait";
    public static final String landscape = "landscape";

    public static String getImageOrientation(String filepath) {
        if (filepath == null) {
            Log.e(TAG, "GPrintCommon : getImageOrientation() : filepath is null!");
            return unknown;
        }

        int imageWidth, imageHeight;

        ExifInterface exif = null;
        try {
            exif = new ExifInterface(filepath);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (exif != null && exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0) > 0) {
            imageWidth = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 1);
            imageHeight = exif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 1);
        } else {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;
            Bitmap image = BitmapFactory.decodeFile(filepath, options);
            if (image == null) {
                Log.e(TAG, "GPrintCommon : getImageOrientation() : image is invalid. " + filepath);
                return unknown;
            }

            imageWidth = image.getWidth();
            imageHeight = image.getHeight();
        }

        if (imageWidth > imageHeight) {
            return landscape;
        }

        return portrait;
    }
}